如果没有属性,PHP对象返回false

如果没有属性,PHP对象返回false,php,class,object,properties,logic,Php,Class,Object,Properties,Logic,我试图创建一个包含一组属性的类。它将这样使用: $class = new test_class(); $class->property; $class->second_property; 基本上,如果属性存在,则它们为真;如果属性不存在,则它们为假。这些属性没有价值,只有存在 现在,我想做这样的事情: $class = new test_class(); var_dump($class->property); // false - property does not ex

我试图创建一个包含一组属性的类。它将这样使用:

$class = new test_class();

$class->property;
$class->second_property;
基本上,如果属性存在,则它们为真;如果属性不存在,则它们为假。这些属性没有价值,只有存在

现在,我想做这样的事情:

$class = new test_class();

var_dump($class->property); // false - property does not exist
var_dump($class->second_property); // true - second_property exists

var_dump( (bool) $class); // true
$user = permissions::get_user(USER_ID_HERE);
// Every property of the $user is a section

var_dump($user->this_section_exists); // true - returns an object
var_dump($user->this_section_doesnt); // false - returns a boolean
因此,如果测试类的一个属性存在,那么
$class
将显示true,因为它是一个对象

但是,在类没有属性的情况下,我希望发生以下情况:

$class = new test_class();

var_dump($class->property); // false - property does not exist
var_dump($class->second_property); // false - second_property does not exist

var_dump( (bool) $class); // false
但是,我仍然希望
$class
成为
测试类的
实例,但在逻辑测试中返回false

这有可能吗?如果是,我会怎么做

谢谢,奥兹

编辑:

为了澄清,我已经在使用_get()神奇函数。我想要发生的是,如果
test\u类
没有属性,那么当对它执行
var\u dump
时,它返回false,但是
instanceof
将返回
test\u类

详细阐述…

我正在创建一个复杂的权限系统。 用户获得分配的分区,每个分区都有一组权限

var_dump($user->this_section_exists); // true
var_dump($user->this_section_exists->this_permission_exists); // true

var_dump($user->this_section_exists->this_permission_doesnt); // false
它的工作原理如下:

$class = new test_class();

var_dump($class->property); // false - property does not exist
var_dump($class->second_property); // true - second_property exists

var_dump( (bool) $class); // true
$user = permissions::get_user(USER_ID_HERE);
// Every property of the $user is a section

var_dump($user->this_section_exists); // true - returns an object
var_dump($user->this_section_doesnt); // false - returns a boolean
如果存在节,则返回节权限的对象

var_dump($user->this_section_exists); // true
var_dump($user->this_section_exists->this_permission_exists); // true

var_dump($user->this_section_exists->this_permission_doesnt); // false
以下是边缘情况:

var_dump($user->this_section_doesnt); // false

var_dump($user->this_section_doesnt->some_permission);
// This should also return false, which it does,
// But it throws a notice: "Trying to get property of non-object"
我希望能够在不修改调用该类的代码的情况下抑制该通知,即,不抑制@。。或者能够返回一个没有属性的对象,该对象在逻辑测试中的计算结果为false

你可以试试


您可以使用进行一些包装来操纵答案

比如说

class test_class {

  private $_validProperties;
  public function __construct()
  {
    $this->validProperties = array('foo', 'bar', 'widget');
  }

  public function __get($prop)
  {
    if (isset($this->_validProperties[$prop])
    {
      return true;
    }
    return false;
  }
}

$a = new test_class();
var_dump($a->foo); //true
var_dump($a->tree); //false

不,你不能完全按照你写的那样做。但你可以模仿类似的东西:

class A {

    public function __toString() { // this is what you want, but it only works when you use your object as a string, not as a bool.
        if (count(get_class_vars(__CLASS__))) { // so if the class has at least 1 attribute, it will return 1.
            return '1';
        } else {
            return '0';
        }
    }
}

$a = new A;
var_dump((bool)(string)$a); // false
如果我在类中添加属性,它将返回true。您也可以在不使用
(bool)
的情况下使用它

如果您不想使用
(string)
,您还有一个选项,可以创建一个包含
\uu toString()
中代码的方法,并调用它而不是强制转换

参考资料:

PS:关于您所说的-对对象执行
var\u dump()
以返回false。不,那是不可能的

我想要发生的是,如果test_类没有属性,那么当对其执行var_转储时,它将返回false,但instanceof将返回test_类


这是不可能的,
var\u dump
将始终显示正确的类型,这就是它的用途。

如果我理解正确,我只能这样做

希望它能帮助你

<?php
class Permissions {
    private $userPermissions = array(
        1 => array(
            'Blog' => array('Post'),
        ),
    );

    private $permission;

    public function __construct($id) {
        $this->permission = $this->userPermissions[$id];
    }

    public function __get($name) {
        if(isset($this->permission[$name])) {
            return new $name($this->permission[$name]);
        }
        return new Notfound();
    }

}

class Blog {
    private $permission;

    public function __construct($permission) {
        $this->permission = $permission;
    }

    public function __get($name) {
        if(($key = array_search($name, $this->permission)) !== false) {
            return new $name();
        }
        return new Notfound();
    }

    public function __tostring() {
        return true;
    }
}

class Post {
    public function __get($name) {
        return isset($this->$name) ? true : new Notfound();
    }
    public function __tostring() {
        return true;
    }
}

class Notfound {
    public function __get($name) {
        return false;
    }
    public function __tostring() {
        return false;
    }
}

$user = new Permissions(1);
var_dump('Blog ', $user->Blog); // return Blog
var_dump('Blog:Post', $user->Blog->Post); // return Post
var_dump('News', $user->News); // return Notfound
var_dump('News:Post', $user->News->Post); // return false

您是否正在寻找类似
类a{public function is_valid(){return$this->a&&$this->b}
?您能否详细说明为什么要使用此功能?看起来这个概念本身是有缺陷的,所以可能有更好的选择。如果没有合适的背景,这个问题是假的,请详细说明你为什么需要这种行为。将背景添加到问题中好的,这很好,直到我们进入问题的最后部分,如果
$this->validProperties
没有属性,然后任何属性的
var\u dump
都将为false。在这个场景中,我还希望
var\u dump($a)
为false,但不要在尝试访问
$a
的属性(如
$a->property
)时抛出“尝试获取非对象的属性”的通知。据我所知,您有点被卡住了。您可以先将
$a
设置为false,然后在分配第一个属性之前不实例化它。如果您愿意将其转换为字符串,则可以执行类似“public function”的操作uuu toString(){If(count($this->_validProperties)==0){return'false';}return'true'}