English 中文(简体)
• 如何将两种类别合并到购买力平价中?
原标题:How to merge two class in PHP?
  • 时间:2010-03-03 20:47:17
  •  标签:
  • php
  • class

为了让最终的类具备两个类中所有的成员变量/方法。

是否有一条容易的道路?

问题回答

你应该将这两个类封装在一个容器类中,并提供一个相关的接口(例如私有变量的设置器/获取器)。

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() 的对象对应项。





相关问题
Brute-force/DoS prevention in PHP [closed]

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 ...

please can anyone check this while loop and if condition

<?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 = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

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 ...

Text as watermarking in PHP

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?

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 ...

热门标签