douwang9650 2012-05-06 16:37
浏览 24

如何在对象中找到最后一个链属性?

To avoid getting the error message as in this previous question, I decided to change the class with __get() like this below,

class property 
{

    public function __get($name)
    {
        return isset($this->$name) ? $this->$name : new property;
    }
}



class objectify
{

    public function array_to_object($array = array(), $property_overloading = false)
    {
        # if $array is not an array, let's make it array with one value of former $array.
        if (!is_array($array)) $array = array($array);

        # Use property overloading to handle inaccessible properties, if overloading is set to be true.
        # Else use std object.
        if($property_overloading === true) $object = new property();
            else $object = new stdClass();

        foreach($array as $key => $value)
        {
            $key = (string) $key ;
            $object->$key = is_array($value) ? self::array_to_object($value, $property_overloading) : $value;
        }


        return $object;

    }
}

$object = new objectify();
$type = null;
$type = $object->array_to_object($type,true);
var_dump($type->a->b->c);

so I get this result in the end,

object(property)#3 (0) { }

but it is still not perfect. as my understanding, the above solution processes the object in a chain like this,

$type = object{}->object{}->object{}

so I wonder if I can find whether it is the last chain and it is empty then just output a null?

$type = object{}->object{}->NULL

is it possible with PHP?

EDIT:

I have thought of an idea which is to count how many times the property class has been instantiated,

class property 
{
    public static $counter = 0;

    function __construct() {
        self::$counter++;
    }

    public function __get($name)
    {
        if(isset($this->$name))
        {   
            return $this->$name;
        }
        elseif(property::$counter < 3)
        {
            return new property;
        }
        else
        {
            return null;
        }

    }
}

but my only problem is how to make the number 3 dynamic. Any ideas?

  • 写回答

1条回答 默认 最新

  • dongtang5776 2012-05-06 17:27
    关注

    Sounds like you're looking for a PHP version of Groovy's ?. operator: http://groovy.codehaus.org/Null+Object+Pattern

    Afaik, you can't overload or create a new operator in PHP. You could perhaps simulate it by passing all of your nested calls to a function, and the function knows when to return null.

    Edit: other options posted here - http://justafewlines.com/2009/10/groovys-operator-in-php-sort-of/

    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值