note 109721 deleted from language.oop5.inheritance by danbrown

From: Date: Fri, 31 Aug 2012 16:33:11 +0000
Subject: note 109721 deleted from language.oop5.inheritance by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-190384@lists.php.net to get a copy of this message
Note Submitter: nanhe dot kumar at gmail dot com ---- /** 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 ?>

« previous php.notes (#190384) next »