glenelkins Posted June 1, 2009 Share Posted June 1, 2009 Hi I must be getting boring on this forum now, i just keep coming accross new things i have'nt used in the past and just need some quick advice. So in a class you have variables that can be used with the pointer $this. but what actually is the main difference between a "private" decleration, and a "protected" Link to comment https://forums.phpfreaks.com/topic/160485-differnet-class-vars/ Share on other sites More sharing options...
PFMaBiSmAd Posted June 1, 2009 Share Posted June 1, 2009 As always, the php.net documentation is the source of all the basic how to/what is php information - Protected limits access to inherited and parent classes (and to the class that defines the item). Private limits visibility only to the class that defines the item. Link to comment https://forums.phpfreaks.com/topic/160485-differnet-class-vars/#findComment-846901 Share on other sites More sharing options...
aschk Posted June 1, 2009 Share Posted June 1, 2009 Simple example... <?php class A { private $myprivate = "private"; protected $myprotected = "protected"; } class B extends A { public function getPrivate(){ return $this->myprivate; } public function getProtected(){ return $this->myprotected; } } $test = new B(); $test->getProtected(); $test->getPrivate(); ?> If you run the above code you will see that class B cannot use the private variable from the class it extended, however it CAN use the protected variable. Link to comment https://forums.phpfreaks.com/topic/160485-differnet-class-vars/#findComment-846909 Share on other sites More sharing options...
glenelkins Posted June 1, 2009 Author Share Posted June 1, 2009 ok so then heres another one for you. can i make the __construct() function abstract? Link to comment https://forums.phpfreaks.com/topic/160485-differnet-class-vars/#findComment-846911 Share on other sites More sharing options...
Ken2k7 Posted June 1, 2009 Share Posted June 1, 2009 Yeah, if you really want to. For certain things like this, why not just write a sample script and test it yourself? Link to comment https://forums.phpfreaks.com/topic/160485-differnet-class-vars/#findComment-846919 Share on other sites More sharing options...
glenelkins Posted June 1, 2009 Author Share Posted June 1, 2009 because im being lazy! i do know after testing if i want to use __construct as abstract, i have to actually have to replace the constructor of the abstract class with the same name function like the old way for example Abstract class Test { function Test() { do constructor bits } Abstract function __construct(); } Link to comment https://forums.phpfreaks.com/topic/160485-differnet-class-vars/#findComment-846945 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.