dsgft1486 2016-01-06 22:31
浏览 118

即时添加方法到stdClass对象

I want to add a function on an object that returns a specific value from that object.

I have the following code now:

    <?php
namespace sdk;
use core\Sdk;

class Contents extends Sdk {

    public $url;
    public $id;

    public function __call($method, $args) {
        if (isset($this->$method)) {
            $func = $this->$method;
            return call_user_func_array($func, $args);
        }
    }

    public function getContents($id = '') {
        $request = 'contents/list/'.$id;
        return $this->getRequest($request);
    }//getContents

    public function getContent($id) {
        $request = 'contents/detail/'.$id;
        $this->object = $this->getRequest($request);
        $this->object->getItemAttribute = function($attribute) {
            var_dump($this->object);
        };
        return $this->object;
    }//getContent

}//Class Contents

and I try to start the method via tis code

$contents->getContent(1);
$contents->object->getItemAttribute('titel');

But I keep getting the error "undefined method"

What am I doing wrong?

  • 写回答

1条回答 默认 最新

  • doutangtan6386 2016-01-06 23:36
    关注

    PHP is not Javascript. You cannot add methods to already existing instances of a class (or methods to an already-defined class, for that matter).

    Here's some alternative ideas:

    1. Use properties that are functions (that's in principle what you are doing now, just with a different invocation).

      $object->getItemAttribute = function($a) {
          return $object->{$a};
      }
      call_user_func($object->getItemAttribute);
      

      I actually would not recommend implementing this, as it will probably make your code difficult to comprehend and annoy the hell out of any future maintainer (including yourself).

    2. Define a wrapper class that implements your new functions:

      class RequestWithAdditionalAttribute {
          private $wrapped;
          public function __construct(\stdClass $wrapped) {
              $this->wrapped = $wrapped;
          }
          public function getItemAttribute($a) {
              return $this->wrapped->{$a};
          }
      }
      
      $wrappedObject = new RequestWithAdditionalAttribute($object)
      
    3. PHP 7 variant: Create an anonymous class:

      $wrappedObject = new class($object) {
          private $wrapped;
          public function __construct(\stdClass $wrapped) {
              $this->wrapped = $wrapped;
          }
          public function getItemAttribute($a) {
              return $this->wrapped->{$a};
          }
      };
      
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题