atitthaker Posted March 1, 2007 Share Posted March 1, 2007 <? class A { protected $b=20; private $a=10; protected function access() { print("here"); } } class B extends A { public function test() { $this->access() ; } } class C extends B { function temp() { $this->access(); } } $abc= new C(); $abc->temp(); ?> I have above piece of code... When I am executing it, it generates output "here"... Now as the statement is inside a protected method of class A, which is directly not accessible by class C as it is not inheriting class A directlty(through class B though). This script is suppose to generate error, and protected method of class A shall not be accessible by class C. But it is accessing it, and printing value... Why it is so??? Isn't it violation of OOP concept?? Link to comment https://forums.phpfreaks.com/topic/40660-protected-member-access/ Share on other sites More sharing options...
itsmeArry Posted March 1, 2007 Share Posted March 1, 2007 keyword protected • available within the class and all children have a look at this page.... http://www.warpstock.net/wse2006/downloads/INT09/oop.pdf Link to comment https://forums.phpfreaks.com/topic/40660-protected-member-access/#findComment-196735 Share on other sites More sharing options...
atitthaker Posted March 1, 2007 Author Share Posted March 1, 2007 As far as I know and understand, according to OOP concept, protected member shall be accessible to it's next level child but not further. So in the example below, it shall be accessible to class B but not class C. In child class protected member's visibility shall reduce to private and shall not be accessible in further children... Isn't it the case?? Link to comment https://forums.phpfreaks.com/topic/40660-protected-member-access/#findComment-196739 Share on other sites More sharing options...
itsmeArry Posted March 1, 2007 Share Posted March 1, 2007 yes thats true but you are accessing the p7ublic function temp of c with its reference and from there its accessing the protected function access. but it you try to access the function access with the reference of class c it will not access that function. Link to comment https://forums.phpfreaks.com/topic/40660-protected-member-access/#findComment-196756 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.