PHP好的方法书写不同名但方法相同的类。

比如说:

class A {
    private $value = null;
    public function __construct() {
        $this->value = 1;
    }
}

class B {
    private $differentValue = null;
    public function __construct() {
        $this->differentValue = 1;
    }
}

A和B的作用完全不同, 他们两个没有办法合并。

class C extends A {
    public function display() {
        echo "OK";
    }
}

class D extends B {
    public function display() {
        echo "OK";
    }
}

C和D的内容又完全相同, 这样的情况有没有什么好的方法把C和D的内容只写一遍?

因为要向下兼容5.3的, 所以trait不能用~~~

阅读 3.6k
3 个回答
class A {
private $value = null;
public function __construct() {
$this->value = 1;
}
}

class B {
private $differentValue = null;
public function __construct() {
$this->differentValue = 1;
}
}

/**
* @method void display()
*/
class C extends A {
private $handler = null;
public function __construct() {
parent::construct();
$this->handler = new E();
}
public function __call($name, $params) {
if ( method_exists($this->handler, $name) ) {
return call_user_func_array(array($this->handler, $name), $params);
} else {
throw new Exception('Method "'.$name.'" does not exists.');
}
}
}

/**
* @method void display()
*/
class D extends B {
private $handler = null;
public function __construct() {
parent::construct();
$this->handler = new E();
}
public function __call($name, $params) {
if ( method_exists($this->handler, $name) ) {
return call_user_func_array(array($this->handler, $name), $params);
} else {
throw new Exception('Method "'.$name.'" does not exists.');
}
}
}

Class E {
public function display() {
echo "OK";
}
}

http://3v4l.org/ns6q7

<?php
class A
{
    public $name   = 'a';
    private $value = null;
    public function __construct()
    {
       $this->value = 1;
    }
}

class B
{
    public $name            = 'b';
    private $differentValue = null;
    public function __construct()
    {
        $this->differentValue = 1;
    }
}

class CD
{
    private $parent = null;
    public function __construct($parent)
    {
        $this->parent = new $parent;
    }

    public function __call($name, $params)
    {
        if ( is_callable(array($this->parent, $name)) ) {
            return call_user_func_array(array($this->parent, $name), $params);
        } else {
            throw new Exception('Method "'.$name.'" should be callable.');
        }
    }

    public function __get($property_name){
        if(isset($this->$property_name)) {
            return $this->$property_name;
        } elseif (isset($this->parent->$property_name)) {
            return $this->parent->$property_name;
        } else {
            return null;
        }
    }

    public function display()
    {
        echo "OK";
    }

}


$c = new CD(new B);

var_dump($c->name);
class A
{
}

class B
{
}

class Proxy
{
    private $instance;

    public function __construct($instance)
    {
        $this->instance = $instance;
    }

    protected function getDisplayText()
    {
        return 'OK';
    }
    
    public function display()
    {
        echo $this->getDisplayText().' [From '.get_class($this->instance).']';
    }
}

class C extends A
{
    private $proxy;

    public function __construct()
    {
        $this->proxy = new Proxy($this);
    }
    
    public function __call($name, $args)
    {
        if(method_exists($this->proxy, $name) === true)
            return call_user_func_array([$this->proxy, $name], $args);
    }
}

class D extends B
{
    private $proxy;

    public function __construct()
    {
        $this->proxy = new Proxy($this);
    }

    public function __call($name, $args)
    {
        if(method_exists($this->proxy, $name) === true)
            return call_user_func_array([$this->proxy, $name], $args);
    }
}

$c = new C();
$c->display();
echo(PHP_EOL);
$d = new D();
$d->display();
OK [From C]
OK [From D]
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题