Jump to content

Need help using namespaces


eldan88

Recommended Posts

Hey Guys. I am new to using namespaces. I am trying to use it on the code below but it keeps giving me an error message saying Class 'lesson_one\Lecture' not found

I am not sure what I am doing wrong here... I have provided an example below. I know there is a duplicate conditional statement. But I am just working with this code to test out the namespace.

Any help would be really appreciated!

namespace lesson_one;
abstract class Lesson {

    protected $duration;
    const  FIXED = 1;
    const TIMED  = 2;
    private $costtype;

    function  __construct($duration, $costtype=1) {
        $this->duration = $duration;
        $this->costtype = $costtype;
    }

    function cost(){
        switch($this->costtype) {
            case self::TIMED:

        return (5* $this->duration);
        break;

       case self::FIXED:
        return 30;
        break;

       default:
       $this->costtype = self::FIXED;
        return 30;
        }

    }


    function chargeType() {
        switch($this->costtype) {
            case SELF::TIMED:
            return "hourly rate";
            break;


            case self::FIXED :
             return "fixed rate";
            break;

            default:
            $this->costtype = self::FIXED;
            return "fixed rate";
        }
    }

}


class Lecture extends  Lesson {

}

class Seminar extends Lesson {

}


//echo Lesson::FIXED;


$lecture = new \lesson_one\Lecture("5", Lesson1::FIXED);
echo $lecture->cost(). "<br>";
echo $lecture->chargeType(). "<br>";
Link to comment
Share on other sites

Also I even tried adding the namespace to the extended class and my IDE says "Unnecessary fully qualified name"

namespace lesson_one;
abstract class Lesson {
// Some code here
}



class Lecture extends  \lesson_one\Lesson {

}
Link to comment
Share on other sites

Hi Eldan,

At the top of your class file you establish that the namespace is lesson_one.

 

This means that until such time as you change namespace, you are working in the lesson_one namespace.

 

Thus it makes no sense that you would refer to specific names paths later in the same file.

 

If you're including that in a script, then the namespace specifier would make sense.

 

Also, as Kevin pointed out, Autoloaders make all of this detail superfluous, and you wouldn't use the namespace to access those either, because they'd be autoloader and available typically with the "use" keyword.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.