web4 Posted April 8, 2010 Share Posted April 8, 2010 /* /* "Private" methods and properties are only visible within the class that defines them, /* "Protected" methods and properties are visible to both their defining class and any /* child(inherited) classes. /* Attempts to access these properties or methods outside their visible area typically /* produces a fatal error that stops script execution. /* /* /* */ <?php class Mammal { public $name; protected $age; private $species; } class Human extends Mammal { } $mammal = new Mammal; echo $mammal->name = 'William'; //ok echo $mammal->age = 3; //fatal error echo $mammal->species = 'Whale'; //fatal error $human = new Human; echo $human->name = 'Barry'; //ok echo $human->age = 1; //fatal error echo $human->species = 'Boy'; //undefined ?> I am confuse on that example about OOP can anyone explain to me why the $mammal->species = 'Whale'; gets fatal error and $human->species = 'Boy'; gets undefined only? because they are the same private and if you get the definition of private from the comments above private is only "visible on it's class only" and so in the code $human->species = 'Boy'; should get also fatal error because the species variable is private to mammal class and even though it is inherited by the human class it should get fatal error because species is private in mammal class.. I hope i delivered my question clearly thanks. Quote Link to comment https://forums.phpfreaks.com/topic/197960-help-in-oop/ Share on other sites More sharing options...
KevinM1 Posted April 8, 2010 Share Posted April 8, 2010 It throws an undefined error with the human becaue private members ARE NOT passed to child classes. The species field doesn't exist in human. It is, by definition, undefined. The fatal errors are a result of attempting to access something that DOES exist, but which you don't have permission to interact with. Quote Link to comment https://forums.phpfreaks.com/topic/197960-help-in-oop/#findComment-1038787 Share on other sites More sharing options...
web4 Posted April 8, 2010 Author Share Posted April 8, 2010 Thanks for your reply but in the human class with the declaration $human->species = 'Boy'; it tries to access the species private variable and thus must have an fatal error... cause from my learning OOP i thought that if you have a parent class and a child class you can inherit the properties of the parent class(ex..all the variables are public) and my conclusion to that is you can access variables in parent class using the child class ) Quote Link to comment https://forums.phpfreaks.com/topic/197960-help-in-oop/#findComment-1038794 Share on other sites More sharing options...
ignace Posted April 8, 2010 Share Posted April 8, 2010 it tries to access the species private variable and thus must have an fatal error... No. The variable $species does NOT exist within Human to show you try: $mammal->age = 1;//Fatal Error $mammal->species = 'Whale';//Fatal Error $human->age = 1;//Fatal Error $human->species = 'Boy';//Undefined Only public and protected fields are passed by inheritance private are NOT. Also you can only access variables directly if you declare them public (in which case they are called properties): $human->name = 'George'; However you should avoid this and use setters (mutators) and getters (accessors) instead. And declare your class members private (only in rare cases should you use protected) class Mammal { private $name; private $age; private $species; public function setName($name) { $this->name = $name; } public function setAge($age) { $this->age = $age; } public function setSpecies($species) { $this->species = $species; } } class Human extends Mammal { } $human = new Human(); $human->setName('George'); $human->setAge(13); $human->setSpecies('Boy'); var_dump($human); Works with no errors. To understand why this works and the other didn't is because of scope resolution the below example also works: class Person { private $name; public function equals(Person $p) { return $p->name === $this->name; } } Quote Link to comment https://forums.phpfreaks.com/topic/197960-help-in-oop/#findComment-1038867 Share on other sites More sharing options...
web4 Posted April 8, 2010 Author Share Posted April 8, 2010 thanks for your reply only one questions remains in mind... echo $mammal->species = 'Whale'; //fatal error i wonder why the error is fatal because the definition of "private" is methods and properties are only visible within the class that defines them however that declaration (in my understanding and i know it is wrong and it needs to be clear up) it shouldn't have a fatal error because $mammal->species that code $mammal is a class right? then we access the private variable with the "same class" through $mammal->species and it shouldn't have an error because this doesn't violate the private definition because i use $mammal->species to access the private variable which means the same class...i hope i deliver it clearly my question... Quote Link to comment https://forums.phpfreaks.com/topic/197960-help-in-oop/#findComment-1038877 Share on other sites More sharing options...
Andy-H Posted April 8, 2010 Share Posted April 8, 2010 thanks for your reply only one questions remains in mind... echo $mammal->species = 'Whale'; //fatal error i wonder why the error is fatal because the definition of "private" is methods and properties are only visible within the class that defines them however that declaration (in my understanding and i know it is wrong and it needs to be clear up) it shouldn't have a fatal error because $mammal->species that code $mammal is a class right? then we access the private variable with the "same class" through $mammal->species and it shouldn't have an error because this doesn't violate the private definition because i use $mammal->species to access the private variable which means the same class...i hope i deliver it clearly my question... $mammal is an object, an instance of the class. Think of it like making your own variables, which can have properties and functions which act on the variables within the object, these variables and functions in OOP are called properties and methods, respectively. When people say that private properties can only be accessed by the class in which they are declared, they mean exactly that, if you want to be able to update the value stored in the variable, you must use a public setter, and in order to retrieve it, a public getter. class mammal { private $_name; private $_age; private $_species; public function setName($name) { $this->_name = $name; } public function setAge($age) { $this->_age = (int)$age; } public function setSpecies($species) { $this->_species = $species; } public function getName() { return $this->_name; } public function getAge() { return $this->_age; } public function getSpecies() { return $this->_species; } // magic method public function __toString() { return $this->_name . ' the ' . $this->_species . ' is ' . $this->_age . ' years old.'; } } $mammal = new mammal(); $mammal->setName('Dillan'); $mammal->setAge('9'); $mammal->setSpecies('Dog'); echo $mammal->getName() . "<br >\r\n"; echo $mammal->getAge() . "<br >\r\n"; echo $mammal->getSpecies() . "<br ><br >\r\n"; echo $mammal; PHP.net on OOP http://uk.php.net/oop http://uk3.php.net/manual/en/language.oop5.magic.php#language.oop5.magic.tostring http://uk3.php.net/__tostring PHPFreaks Tutorial on OOP http://www.phpfreaks.com/tutorial/oo-php-part-1-oop-in-full-effect http://www.phpfreaks.com/tutorial/oo-php-part-2-boring-oo-principles http://www.phpfreaks.com/tutorial/oo-php-part-3-uml-classes-and-relations Hope that helps a little. Quote Link to comment https://forums.phpfreaks.com/topic/197960-help-in-oop/#findComment-1038881 Share on other sites More sharing options...
web4 Posted April 8, 2010 Author Share Posted April 8, 2010 Thank you guys that was much clear. Thank you Andy-H,ignace, Nightslyr Quote Link to comment https://forums.phpfreaks.com/topic/197960-help-in-oop/#findComment-1038900 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.