drwo32555 2018-01-29 15:15
浏览 16
已采纳

我在哪里可以找到这个PHP代码中的“输入”声明? [关闭]

In a php code I found the code snippet:

class GF_Field extends stdClass implements ArrayAccess {

    if ( is_array( $this->inputs ) ) {
        foreach ( $this->inputs as $input ) {

But I don't know what is inputs come from.

Any help would be highly appropriated.

EDIT:

The programmer made two ambiguous step, which made me harder to find the declaration:

  1. Declared an object instance's property in the code as Felippe pointed below
  2. Made an assignment via referenced variable somewhere else in the code:

foreach ( $this->inputs as &$input ) { $input = something

  • 写回答

1条回答 默认 最新

  • duanjucong3124 2018-01-29 15:19
    关注

    You are ommiting code here, but for short answer, you don't have to explicitally define the attributes in PHP. You can create only for the object, and it will work like this:

    class AnyClass extends stdClass {
    
        public function print() {
            if ( is_array( $this->inputs ) ) {
                foreach ( $this->inputs as $input ) {
                    echo $input;
                }
            }
        }
    }
    
    $obj = new AnyClass;
    $obj->inputs = [1,2,3];
    $obj->print();
    

    It will output 123.

    stdClass is a generic class for PHP objects. ArrayAccess is a PHP interface. So, in your case, the $inputsis probably not defined anywhere in the class, or class tree, but is "defined" directly in the object instance.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?