note 70362 rejected from language.oop5.visibility by bobby
| From: | bobby@php.net | Date: | Fri, 13 Oct 2006 02:29:05 +0000 |
| Subject: | note 70362 rejected from language.oop5.visibility by bobby | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-118478@lists.php.net to get a copy of this message | ||
Note Submitter: florante_navaja at yahoo dot com
----
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:
<?php
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