Dynamic extension in PHP
I am new on SO, so I hope I make myself clear in the following question.
I have been struggling myself recently trying to write a framework for a project. The problem is that I am new to PHP OOP and I am 开发者_StackOverflownot sure how to extend an object that can be changed in the future and still keep the properties available in the extended class.
More precisely:
class main {
function __construct() {
$this->a='some a text';
$this->b='some b text';
}
}
class submain extends main() {
function __construct() {
parent::__construct();
$this->c='some c text';
}
}
$main=new main();
$submain=new submain();
So now the $submain variable contains 3 properties a,b and c.
What if now I do $main->d='some d text';
How do I make so that the d property appears in the $submain variable which extended the main class?
I suppose I should be using cloning?!
Starting with the Edited Answer
I now understand the question to be: How can you set a property on one object and have it set in all classes that derive from it (at runtime).
Sorry, I think my best answer can only be a philosophical one.
Certain languages favour certain approaches to solving problems. (Actually in javascript this would be really easy). Writing a class in PHP defines the object that gets created when the class is instantiated. Once an object is created methods of the class and inherited methods can be called to perform actions that the object is designed to do.
Many objects can be created which each encapsulate a part of your system. Making each object do a specific job and keeping its coupling to other parts of the system low enables large systems to be built in specific testable parts.
Each object created through a class is specific (possibly sharing static members - although static may be considered bad design). You would have to force PHP to dynamically share settings between objects of the same class. Now that I have hopefully dissuaded you from doing so - here is how you could do it. Notice that it looks ugly and will cause you infinite problems in the future if your program gets to any size. Please don't use what I am posting here. I didn't test this code or its syntax, I never write static stuff so there are probably mistakes, but I think this idea would work (and be horrible).
class main
{
// Yes its defined statically but with an array you can do anything.
public static $settings;
public function __construct() {}
public defineVar($varName, $varValue)
{
self::$settings[$varName] = $varValue;
}
public static function get($varName)
{
return self::$settings[$varName] = $varValue;
}
}
// Submain as before.
$t = new submain();
main::defineVar('d', 'some d text');
$t->get('d');
This will create a dynamically definable value that you can store in your main and all derived classes. Their values will be the same throughout all objects. If you want a value that can be different for the same variable through the classes you would basically be setting up a registry system (I think there might be a design pattern for that).
Original Answer Below
I would write this with the access types of public, protected and private which are important in OOP.
All objects you create of class main will contain the properties as set by their constructor. Same goes for submain so you would add $d just as an a, b or c entry. I'll add it to the main, the submain constructor already calls its parent constructor so will construct objects with properties a, b, c and d defined.
class main
{
public $a;
protected $b;
public $d;
public function __construct()
{
$this->a='some a text';
$this->b='some b text';
$this->d='some d text';
}
}
class submain extends main()
{
public $c;
public function __construct()
{
parent::__construct();
$this->c='some c text';
}
public function getB()
{
return $this->b;
}
}
$main=new main();
echo $main->a; // Publicly accessible.
// Can't do $main->b() it is protected.
echo $main->d;
$submain=new submain();
echo $submain->a; // Still publicly accessible.
echo $submain->getB(); // protected variable exposed by public function.
echo $submain->d;
You can just set the $d
property in the main
class:
class main {
public $d = 'some d text'; # <- set the property as public here.
function __construct() {
$this->a='some a text';
$this->b='some b text';
}
}
class submain extends main() {
function __construct() {
parent::__construct();
$this->c='some c text';
}
}
$main=new main();
$submain=new submain();
echo $submain->d; # some d text
But I'm a bit unsure what you're looking for exactly. Just ask.
精彩评论