- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我需要了解魔术函数 __isset()
和普通函数 isset()
。实际上 php 语言构造 isset()
和 php 魔术方法 __isset()
之间的真正区别是什么?当我用谷歌搜索时,他们告诉我 __isset()
是一个神奇的功能。 php中常用的php函数和魔术函数有什么区别?
最佳答案
isset()
它是一种检查变量或类属性初始化的语言结构:
$a = 10;
isset($a); // true
isset($a, $b); // false
class Test
{
public $prop = 10;
}
$obj = new Test;
isset($obj->prop); // true
__isset()
这是一个神奇的方法,当 isset()
或 empty()
检查不存在或不可访问的类属性时调用:
class Test
{
public function __isset($name) {
echo "Non-existent property '$name'";
}
}
$obj = new Test;
isset($obj->prop); // prints "Non-existent property 'prop'" and return false
isset() __isset()
Language construct | Magic method |Always return bool | Result depends on custom logic* |Must be invoked in code | Called automatically by event |Unlimited number of parameters | Has only one parameter |Can be used in any scope | Must be defined as method** |Is a reserved keyword | Not a reserved keyword |Can't be redefined (Parse error) | Can be redefined in extended class***
__isset()
结果将是 automatically casted作为 bool
.
其实你可以定义自定义函数__isset()
,但它与魔术方法无关。
见 this example .
与普通函数不同,只能在类范围内定义并在特定事件上自动调用,例如:不可访问的方法调用、类序列化、当 unset()
用于不可访问的属性等。另见官方文档:Overloading .
关于php - isset() 和 __isset() 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21227585/
我需要了解魔术函数 __isset() 和普通函数 isset()。实际上 php 语言构造 isset() 和 php 魔术方法 __isset() 之间的真正区别是什么?当我用谷歌搜索时,他们告诉
我正在尝试使 empty() 和 isset() 等函数处理方法返回的数据。 我目前拥有的: abstract class FooBase{ public function __isset($na
我有一个抽象父类 Mongo_Document(来自 mongodb-php-odm)和一个继承类 Model_ActionPlan。 Mongo_Document 具有神奇的 __isset 和 _
我是一名优秀的程序员,十分优秀!