private vars are not accessible that way. This is the way it works...
private $var is only usable by the class. You can't access it through $object->var.
protected $var is only usable by the class if it was called directly, but not by any objects that are classes that are extended from the base. Example:
class Parent { protected $var = "test"; function blah($somevar) { ... } }
class Child extends Parent { ... }
$parent = new Parent; print $parent->var; //ouputs "test" $child = new Child; print $child->var; //outputs nothing
as you can see $var is only accessible by objects that are specifically Parents, not Children.
public vars are accessible by the class itself, ($this->var), derived classes, and by $class->var.
HTH, Cheers! -Michael Paul Hudson wrote:
All,
I'm toying with the new stuff available in PHP 5 (latest CVS), but I've hit a brick wall: both private and protected don't seem to work as I'd expect them to.
Here's an example script:
<?php class dog { // declare two private variables private $Name; private $DogTag;
public function bark() { print "Woof!\n"; } }
// new class, for testing derived stuff class poodle extends dog { public function bark() { print "Yip!\n"; } }
// I now create an instance of the // derived class $poppy = new poodle;
// and set its private property
$poppy->Name = "Poppy";
print $poppy->Name;
?>
For some reason, that script works fine - PHP doesn't object to me setting private variables in the derived class. Yet if I use "$poppy = new dog", the script errors out as expected. It's almost like PHP inherits the member variables, but not the attached access control.
For protected, here's another script:
<?php class dog { // this next function is protected // viz, it should be available to dog // and its children
protected function bark() { print "Woof!\n"; } }
class poodle extends dog { // nothing happening here }
$mydog = new poodle;
// I now call the protected function
$mydog->bark();
?>
That script errors out saying that I can't call the protected function bark - surely, being protected, it should be available in the poodle class too?
Of course, it might be that these two pieces of functionality are not yet implemented in PHP, or, more likely, that I'm just misinterpreting the documentation! ;)
If you have any insight, please CC me into your response to the list.
Thanks,
Paul
-- Pratt Museum IT Intern All programmers are playwrights and all computers are lousy actors.
-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php