为了让最终的类具备两个类中所有的成员变量/方法。
是否有一条容易的道路?
为了让最终的类具备两个类中所有的成员变量/方法。
是否有一条容易的道路?
你应该将这两个类封装在一个容器类中,并提供一个相关的接口(例如私有变量的设置器/获取器)。
class YourFirstClass {
public $variable;
private $_variable2;
public function setVariable2($a) {
$this->_variable2 = $a;
}
}
class YourSecondClass {
public $variable;
private $_variable2;
public function setVariable2($a) {
$this->_variable2 = $a;
}
}
class ContaingClass {
private $_first;
private $_second;
public function __construct(YourFirstClass $first, YourSecondClass $second) {
$this->_first = $first;
$this->_second = $second;
}
public function doSomething($aa) {
$this->_first->setVariable2($aa);
}
}
研究(谷歌):“组合优于继承”
脚注:对非可变名称表示歉意。
Are you asking to do this at runtime, or at programming time?
I will assume runtime, in which case what is wrong with using class inheritance?
Create a new class which inherits from the two you want to merge.
# Merge only properties that are shared between the two classes into this object.
public function conservativeMerge($objectToMerge)
{
# Makes sure the argument is an object.
if(!is_object($objectToMerge))
return FALSE;
# Used $this to make sure that only known properties in this class are shared.
# Note: You can only iterate over an object as of 5.3.0 or greater.
foreach ($this as $property => $value)
{
# Makes sure that the mering object has this property.
if (isset($objectToMerge->$property))
{
$objectToMerge->$property = $value;
}
}
}
# Merge all $objectToMerge s properties to this object.
public function liberalMerge($objectToMerge)
{
# Makes sure the argument is an object.
if(!is_object($objectToMerge))
return FALSE;
# Note: You can only iterate over an object as of 5.3.0 or greater.
foreach ($objectToMerge as $property => $value)
{
$objectToMerge->$property = $value;
}
}
你应该将第一种方法视为 array_combine()
的对象对应项。然后将第二种方法视为 array_merge()
的对象对应项。
I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...
<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...
I found this script online that creates a thumbnail out of a image but the thumbnail image is created with poor quality how can I improve the quality of the image. And is there a better way to create ...
如何确认来自正确来源的数字。
Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...
I wonder there is a way to post a message to a facebook business page with cURL? thanks
I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...
How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...