note 109721 added to language.oop5.inheritance

From: Date: Tue, 14 Aug 2012 12:26:11 +0000
Subject: note 109721 added to language.oop5.inheritance
Groups: php.notes 
Request: Send a blank email to php-notes+get-190063@lists.php.net to get a copy of this message
/** You can understand much more about inheritance Nanhe Kumar */ <?php error_reporting(E_ALL | E_STRICT); ini_set("display_errors", 1); ?> <?php class A{ public $publicA; protected $protectedA; private $privateA; function __construct($privateA,$protectedA,$publicA){ $this->privateA=$privateA; $this->protectedA=$protectedA; $this->publicA=$publicA; } protected function setPrivate($privateA){ $this->privateA=$privateA; } protected function getPrivate(){ return $this->privateA; } public function Show(){ echo "A::Show()"; echo $this->privateA; echo $this->protectedA; echo $this->publicA; } } class B extends A { function __construct($privateA, $protectedA, $publicA) { parent::__construct($privateA, $protectedA, $publicA); } public function Show(){ echo "B::Show()"; //echo $this->privateA; Invalid because it is a private variable echo $this->getPrivate(); echo $this->protectedA; echo $this->publicA; } public function getProtected() { return $this->protectedA; } public function setProtected($protectedA){ $this->protectedA=$protectedA; } } ?> <?php //Php not support function overload and we have overide default constructor so we can't call like $objA=new A(); $B =new B(2,3,4); $B->Show();//It call Class B Show Function Ouput : B::Show()B::Show()234 // echo $B->protectedA; You can't direct acess protected value echo $B->getProtected(); // Ouput : 3 echo $B->publicA; // Ouput : 4 ?> ---- Server IP: 69.147.83.197 Probable Submitter: 210.7.74.130 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.inheritance.php Edit -- https://master.php.net/note/edit/109721 Del: integrated -- https://master.php.net/note/delete/109721/integrated Del: useless -- https://master.php.net/note/delete/109721/useless Del: bad code -- https://master.php.net/note/delete/109721/bad+code Del: spam -- https://master.php.net/note/delete/109721/spam Del: non-english -- https://master.php.net/note/delete/109721/non-english Del: in docs -- https://master.php.net/note/delete/109721/in+docs Del: other reasons-- https://master.php.net/note/delete/109721 Reject -- https://master.php.net/note/reject/109721 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#190063) next »