note 70361 added to language.oop5.visibility

From: Date: Fri, 13 Oct 2006 02:03:52 +0000
Subject: note 70361 added to language.oop5.visibility
Groups: php.notes 
Request: Send a blank email to php-notes+get-118476@lists.php.net to get a copy of this message
It is said that the constructor must be declared public. for whatever reason, it can be declared protected or private! However, only child classes can make a call to the protected or private parent constructor. Instantiating the parent class will produce an error. In the same manner, a protected __destruct in the parent class is possible, but *must not* be private. Here's the snippet: class Visibility { public function pubFunc() { echo 'pubFunc called'; } protected function protFunc() { echo 'protFunc called '; } private function privFunc() { echo 'privFunc called'; } function privAccessor() { privFunc(); } /*alternate these next two lines */ //private function __construct() { protected function __construct() { echo 'Visibility constructor called'; } // private function __destruct() //fails! protected function __destruct() { //ok! echo 'Visibility destructor called'; } } class ChildVisibility extends Visibility { public function callProtFunc() { parent::protFunc(); } public function __construct() { parent::__construct(); echo 'ChildVisibility constructor called'; } public function __destruct() { echo 'ChildVisibility destructor called'; parent::__destruct(); } } $oCV = new ChildVisibility(); $oCV->callProtFunc(); Here's what i got: Visibility constructor called ChildVisibility constructor called protFunc called ChildVisibility destructor called Visibility destructor called ---- Server IP: 66.163.161.117 Probable Submitter: 218.219.13.70 (proxied: unknown) ---- Manual Page -- http://www.php.net/manual/en/language.oop5.visibility.php Edit -- https://master.php.net/note/edit/70361 Del: integrated -- https://master.php.net/note/delete/70361/integrated Del: useless -- https://master.php.net/note/delete/70361/useless Del: bad code -- https://master.php.net/note/delete/70361/bad+code Del: spam -- https://master.php.net/note/delete/70361/spam Del: non-english -- https://master.php.net/note/delete/70361/non-english Del: in docs -- https://master.php.net/note/delete/70361/in+docs Del: other reasons-- https://master.php.net/note/delete/70361 Reject -- https://master.php.net/note/reject/70361 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#118476) next »