douxuzui4590 2018-04-11 06:52
浏览 56

PHP - 有没有办法区分何时通过PHP构造调用魔术方法或直接?

in PHP, is there is there a way to differentiate when magic method is called through a PHP construct or directly?

E.g.: public function __clone() {} is called each time an object is cloned through the clone keyword:

$a = new ObjectA();
$clone = clone $a; // Calls __clone()

However, someone can even (do not know why someone should do that, but, anyway) call __clone() directly:

$a = new ObjectA();
$clone = $a->__clone(); // Call __clone() directly

Same goes for other PHP magic methods (__call(), __debugInfo, etc.).

Can we differentiate between the two cases somehow?

Thank you for the attention!

EDIT: Assume the following scenario:

I have a dynamic proxy class, which extends a base class and internally creates an instance of the base class only when the object is effectively used:

class A {
   ...
   public function __clone() {
      echo "clone A!!!";
   }
   ...
}

class DynamicProxy extends A {

   protected $a = NULL;

   public function __clone() {
      $this->createA();
      if ( /* __clone() is triggered by `clone $obj`, I need to clone $a: */ )  {
        clone $this->a;
      }
      else {
        // But if someone, for some reason (I do not now why, but it could be, and I would like to be consistent), have called `$obj->__clone();`, I do not have to call `clone $obj`, but proxy the call:
        return $this->a->__clone();
      }

   }

   protected function createA() {
      if (is_null($this->a)) {
        $this->a = new A();
      }
   }
}

$obj = new DynamicProxy();
  • 写回答

1条回答 默认 最新

  • doupin1073 2018-06-04 11:48
    关注

    Many of your assumptions are incorrect, one can not call __clone() directly and get a clone of the object to be returned because the docs says:

    void __clone ( void )
    

    and

    An object's __clone() method cannot be called directly.

    It is a bit misleading because it can actually be called, but the result is like calling any other object's method, and will not make a clone of the original object.

    Consider this snippet:

    <?php
    class Foo {
        function __construct($x) {
            $this->x = $x;
        }
    
        function __clone() {
            echo "I'm a clone";
        }
    
        function getX() {
            echo $this->x;
        }
    }
    
    $a = new Foo('foo');
    $b = $a->__clone(); # > I'm a clone
    var_dump($b);       # > NULL
    $b->getX();         # Exception: Call to a member function getX() on null
    
    ?> 
    

    __clone method is called on newly created, clone object after it has been cloned. It's more like a callback and it does not produce a clone.

    Maybe it would be better if PHP threw an exception when someone tried to call __clone directly, but there are many things PHP could do better.

    What you can do is to deeply test if what you think is possible is really possible.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度