我正在尝试使一个类包含一组属性。 它将像这样使用:
$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
因此,即使测试类的一个属性存在,var 转储$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
instanceof
test_class
但在逻辑测试中返回 false。
这可能吗?如果是这样,我该怎么做?
谢谢,奥兹
编辑:
澄清一下,我已经在使用 __get() 魔术函数。我想发生的是,如果test_class
没有属性,那么当对其执行var_dump
时,它返回 false,但instanceof
将返回 test_class
。
阐述。。。
我正在创建一个复杂的权限系统。用户将获得分配的部分,每个部分都有一组权限。
它将像这样工作:
$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。
我想发生的是,如果test_class没有属性,那么当对其执行var_dump时,它返回 false,但实例将返回test_class。
这是不可能的,var_dump
总是会向你显示正确的类型,这就是它的目的。
不,你不能完全按照你写的去做。但是你可以模拟类似的东西:
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
如果我在 A 类中添加一个属性,它将返回 true。您也可以在没有(bool)
的情况下使用它。
if ((string)$a) {
echo 'I am not empty';
} else {
echo 'I am an empty object';
}
如果您不想使用 (string)
,则剩下一个选项,即创建一个包含 __toString()
中的代码的方法并调用它而不是强制转换。
引用:
- __toString()
- get_class_vars()
-
__CLASS__
PS:关于你所说的 - 对对象进行var_dump()
以返回 false。不,这是不可能的。
您可以使用 __get() 进行一些包装来操作答案。
说类似的话
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
你可以试试PHP魔术函数
<?php
class Test {
private $name = 'name';
public $age = 20;
public function __get($name) {
return isset($this->$name) ? true : false;
}
public function __toString() {
return true;
}
}
$object = new Test();
var_dump($object->name); // output `true`
var_dump($object->age); // output `20`
var_dump($object->notfound); // output `false`
var_dump((bool)$object); // output `true`
如果我理解正确,我只能这样做。
希望它能帮助你
<?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