dongzhong7299 2016-12-10 10:44
浏览 19
已采纳

使用匿名函数分配属性值

My class is like this:

<?php

class ExampleClass{

    private $example_property = false;

    public function __construct(){
        $this->example_property = function() {
            $this->example_property = 1;
            return $this->example_property;
        };
    }

    public function get_example_property(){
        return $this->example_property; 
    }
}

$example = new ExampleClass();
echo $example->get_example_property();

Property $example_property must be false until you call it, then, the first time it is called, I want to assign 1 to it. What's wrong with my code?

Error: Error Object of class Closure could not be converted to string on line number 20.

  • 写回答

1条回答 默认 最新

  • dongyi5425 2016-12-10 11:14
    关注

    I just tried to play a little bit with your code.

    To make it possible, you'll have to find out, whether your variable is a function (defined in your __construct) or the number set by this function

    <?php
    
    class ExampleClass{
    
        private $example_property = false;
    
        public function __construct(){
            $this->example_property = function() {
                $this->example_property = 1;
                return $this->example_property;
            };
        }
    
        public function get_example_property(){
            $func = $this->example_property;
            if(is_object($func) && get_class($func)=='Closure'){
                return $func();
            }
            return $func;
        }
    }
    
    $example = new ExampleClass();
    echo $example->get_example_property(); //returning 1
    echo $example->get_example_property(); //returning 1 again
    

    But anyway, I don't see any sense in doing this.

    The typical solution would be something like this:

    class ExampleClass{
    
        private $example_property = false;
    
        public function __construct(){
            //usually initializing properties goes here.
            //$this->example_property = 1;
        }
    
        public function get_example_property(){
            // I think you want a value to be initialzed only if needed.
            // so then it can go here.
            if(!$this->example_property) {
                $this->example_property = 1; //initialize it, if needed
            }
            return $this->example_property;
        }
    }
    
    $example = new ExampleClass();
    echo $example->get_example_property(); //returning 1
    echo $example->get_example_property(); //returning 1 again
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵