3

I'm learning PHP and I'm stuck i the following code:

<?php

class dogtag {

    protected $Words;

}

class dog {

    protected $Name;
    protected $DogTag;

    protected function bark() {
        print "Woof!\n";
    }

}

class poodle extends dog {

    public function bark() {
        print "Yip!\n";
    }

}

$poppy = new poodle;
$poppy->Name = "Poppy";
$poppy->DogTag = new dogtag;
$poppy->DogTag->Words = "My name is
Poppy. If you find me, please call 555-1234";

var_dump($poppy);

?>

This is what I got:

PHP Fatal error:  Uncaught Error: Cannot access protected property poodle::$Name

This looks strange to me as I should access protected vars and functions from child classes.

Could please someone explain where I'm wrong?

Many thanks.

0

3 Answers 3

2

Protected variables can indeed be accessed from the child class. However you aren't accessing your variable from inside the child class.

If you make the variables public you can access them from outside the class.

Documentation: http://php.net/manual/en/language.oop5.visibility.php

Example:

Class Dog {

   private $privateProperty = "private"; //I can only be access from inside the Dog class
   protected $protectedProperty = "protected"; //I can be accessed from inside the dog class and all child classes
   public $publicProperty = "public"; //I can be accessed from everywhere.

}


Class Poodle extends Dog {

   public function getProtectedProperty(){
       return $this->protectedProperty; //This is ok because it's inside the Poodle (child class);
   }

}

$poodle = new Poodle;
echo $poodle->publicProperty; //This is ok because it's public
echo $poodle->getProtectedProperty(); //This is ok because it calls a public method.
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks @Daan but I not sure I get it. You mean I'm trying to access it from an instance ($poppy) of the subclass and not from the sublass definition (class poodle extends...)?
It means you're not accessing it from inside the dog class but from the outside.
I'm very sorry but I don't understand. I'm trying to access $Name from the poodle subclass, which inherits from dog class. I create an instance of poodle ($poppy) and I try to change the protected var $Name. I understand I can't do this with a private var but $Name is protected. So as $poppy is an instance of poodle class, I should be able to modify the protected parent's var. Thanks for you patience.
I've added an example, hopefully this will clear things up.
Thank you very much, now I understand! Have a nice day.,
0

You can't access the property 'Words', you need to make it public

Comments

0

you could add magic methods to your class - that would allow you to access and manipulate the private properties from outside the class.

class foo{
    private $bah;

    public function __construct(){
        $this->bah='hello world';
    }
    public function __get( $name ){
        return $this->$name;
    }
    public function __set( $name,$value ){
        $this->$name=$value;
    }
    public function __isset( $name ){
        return isset( $this->$name );
    }
    public function __unset( $name ){
        unset( $this->$name );  
    }
}

$foo=new foo;
echo $foo->bah;

$foo->bah='banana';
echo $foo->bah;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.