note 79986 added to language.oop5.decon
| From: | Typer85 at gmail dot com | Date: | Sat, 22 Dec 2007 05:31:22 +0000 |
| Subject: | note 79986 added to language.oop5.decon | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-133878@lists.php.net to get a copy of this message | ||
In regards to a Class Constructor visibility ...
I too was having the same problem with Class Constructor visibility, in which I had one Class that
was extended by several other Classes. The problem that I encountered was in one of the Child
Classes, I wanted a weaker visibility. Consider the following example:
<?php
class A {
// Public Constructor ...
// No Problems Here.
public function __construct( ) {
}
// Class Function.
public function functionA( ) {
}
}
class B extends A {
// Public Constructor ...
// Same As Parent Class ...
// Again No Problems Here.
public function __construct( ) {
}
// Class Function.
public function functionB( ) {
}
}
class C extends A {
// Private Constructor ...
// Weaker Then Parent Class ...
// PHP Will Throw A Fatal Error.
private function __construct( ) {
}
// Class Function.
public function functionC( ) {
}
}
?>
Nothing new in the above example that we have not seen before. My solution to solve this problem?
Create an Abstract Class with all the functionality of Class A. Make its Class Constructor have a
visibility of Protected, then extend each of the three Classes above from that Abstract Class. In a
way, the Abstract Class acts as a dummy Class to get rid of the visibility problem:
<?php
abstract class AAbstract {
// Protected Constructor ...
// Abstract Class Can Not Be Created Anyway ...
// No Problems Here.
protected function __construct( ) {
}
// Class Function ...
// Originally In Class A ...
// Which Was Used As A Super Class.
public function functionA( ) {
}
}
class A extends AAbstract {
// Public Constructor ...
// Stronger Than Parent Class ...
// Again No Problems Here.
public function __construct( ) {
// By Moving All The Functionality Of
// Class A To Class AAbstract Class A
// Will Automatically Inherit All Of
// Its Functionality. The Only Thing
// Left To Do Is To Create A Constructor
// Which Calls Class AABstract's Constructor
// To Mimic Similar Behavior.
parent::__construct( );
}
// No Need To Redeclare functionA( )
// Since It Was Moved To Class
// AAbstract.
}
class B extends AAbstract {
// Public Constructor ...
// Stronger Than Parent Class ...
// Again No Problems Here.
public function __construct( ) {
parent::__construct( );
}
// Class Function ...
// Specific To This Class.
public function functionB( ) {
}
}
class C extends AAbstract {
// Protected Constructor ...
// Same As Parent Class ...
// Again No Problems Here.
protected function __construct( ) {
}
// Class Function ...
// Specific To This Class.
public function functionC( ) {
}
}
?>
As you can see the problem is more or less fixed. Class AAbstract acts a dummy class, containing all
the original functionality of Class A. But because it has a protected Constructor, in order to make
its functionality available, Class A is redeclared as Child Class with the only difference of it
having a public Constructor that automatically calls the Parent Constructor.
Notice that Class B does not extend from Class A but also from Class AAbstract! If I wanted to
change Class B's Constructor to protected, I can easily do it! Notice that an extra Method was
added to Class B ... this is because Class B has extra functionality specific to itself. Same
applies to Class C.
Why don't Class B and Class C extend from Class A? Because Class A has a public Constructor,
which pretty much defies the point of this solution.
This solution is not perfect however and has some flaws.
Good Luck,
----
Server IP: 69.147.83.197
Probable Submitter: 217.54.226.158
----
Manual Page -- http://www.php.net/manual/en/language.oop5.decon.php
Edit -- https://master.php.net/note/edit/79986
Del: integrated -- https://master.php.net/note/delete/79986/integrated
Del: useless -- https://master.php.net/note/delete/79986/useless
Del: bad code -- https://master.php.net/note/delete/79986/bad+code
Del: spam -- https://master.php.net/note/delete/79986/spam
Del: non-english -- https://master.php.net/note/delete/79986/non-english
Del: in docs -- https://master.php.net/note/delete/79986/in+docs
Del: other reasons-- https://master.php.net/note/delete/79986
Reject -- https://master.php.net/note/reject/79986
Search -- https://master.php.net/manage/user-notes.php