/** PHP把所有以__(两个下划线)开头的类方法当成魔术方法。所以你定义自己的类方法时,不要以 __为前缀。 * */
// __toString、__set、__get__isset()、__unset()
/*
The __toString method allows a class to decide how it will react when it is converted to a string.
__set() is run when writing data to inaccessible members.
__get() is utilized for reading data from inaccessible members.
__isset() is triggered by calling isset() or empty() on inaccessible members.
__unset() is invoked when unset() is used on inaccessible members.
*/
class TestClass {
private $data = array();
public $foo;
public function __construct($foo) {
$this->foo = $foo;
}
public function __toString() {
return $this->foo;
}
public function __set($name, $value) {
echo "__set, Setting '$name' to '$value'\n";
$this->data[$name] = $value;
}
public function __get($name) {
echo "__get, Getting '$name'\n";
if (array_key_exists($name, $this->data)) {
return $this->data[$name];
}
}
/** As of PHP 5.1.0 */
public function __isset($name) {
echo "__isset, Is '$name' set?\n";
return isset($this->data[$name]);
}
/** As of PHP 5.1.0 */
public function __unset($name) {
echo "__unset, Unsetting '$name'\n";
unset($this->data[$name]);
}
}
$obj = new TestClass('Hello');
echo "__toString, $obj\n";
$obj->a = 1;
echo $obj->a . "\n\n";
var_dump(isset($obj->a));
unset($obj->a);
var_dump(isset($obj->a));
echo "\n\n";
/**
输出结果如下:
__toString, Hello
__set, Setting 'a' to '1'
__get, Getting 'a'
__isset, Is 'a' set?
bool(true)
__unset, Unsetting 'a'
__isset, Is 'a' set?
bool(false)
**/
// __call __callStatic
/*
mixed __call ( string $name , array $arguments )
mixed __callStatic ( string $name , array $arguments )
__call() is triggered when invoking inaccessible methods in an object context.
__callStatic() is triggered when invoking inaccessible methods in a static context.
The $name argument is the name of the method being called.
The $arguments argument is an enumerated array containing the parameters passed to the $name'ed method.
*/
class MethodTest {
public function __call($name, $arguments) {
// Note: value of $name is case sensitive.
echo "__call, Calling object method '$name' " . implode(', ', $arguments) . "\n";
}
/** As of PHP 5.3.0 */
public static function __callStatic($name, $arguments) {
// Note: value of $name is case sensitive.
echo "__callStatic, Calling static method '$name' " . implode(', ', $arguments) . "\n";
}
}
$obj = new MethodTest;
$obj->runTest('in object context', 'param2', 'param3');
//MethodTest::runTest('in static context'); // As of PHP 5.3.0
echo "\n\n";
/**
输出结果如下:
__call, Calling object method 'runTest' in object context, param2, param3
string(10) "__invoke: "
*/
// __invoke
/*
The __invoke method is called when a script tries to call an object as a function.
Note: This feature is available since PHP 5.3.0.
*/
class CallableClass {
function __invoke($x) {
var_dump($x);
}
}
$obj = new CallableClass;
//$obj(5);
var_dump('__invoke: ' . is_callable($obj));
echo "\n\n";
// __sleep __wakeup
/*
串行化serialize可以把变量包括对象,转化成连续bytes数据. 你可以将串行化后的变量存在一个文件里或在网络上传输.
然后再反串行化还原为原来的数据. 你在反串行化类的对象之前定义的类,PHP可以成功地存储其对象的属性和方法.
有时你可能需要一个对象在反串行化后立即执行. 为了这样的目的,PHP会自动寻找__sleep和__wakeup方法.
当一个对象被串行化,PHP会调用__sleep方法(如果存在的话). 在反串行化一个对象后,PHP 会调用__wakeup方法.
这两个方法都不接受参数. __sleep方法必须返回一个数组,包含需要串行化的属性. PHP会抛弃其它属性的值.
如果没有__sleep方法,PHP将保存所有属性.下面的例子显示了如何用__sleep和__wakeup方法来串行化一个对象.
Id属性是一个不打算保留在对象中的临时属性. __sleep方法保证在串行化的对象中不包含id属性.
当反串行化一个User对象,__wakeup方法建立id属性的新值. 这个例子被设计成自我保持.
在实际开发中,你可能发现包含资源(如图像或数据流)的对象需要这些方法
*/
class User {
public $name;
public $id;
function __construct() {
//give user a unique ID 赋予一个差别 的ID
$this->id = uniqid();
}
//__sleep返回值的类型是数组,数组中的值是不需要串型化的字段id
function __sleep() {
//do not serialize this->id 不串行化id
return(array("name"));
}
function __wakeup() {
//give user a unique ID
$this->id = uniqid();
}
}
//create object 成立一个器材
$u = new User;
$u->name = "Leon"; //serialize it 串行化 留意不串行化id属性,id的值被遗弃
$s = serialize($u);
echo "__sleep, __wakeup, s: $s"; //unserialize it 反串行化 id被重新赋值
$u2 = unserialize($s); //$u and $u2 have different IDs $u和$u2有差别 的ID
print_r($u);
print_r($u2);
echo "\n\n";
/**
输出结果如下:
__sleep, __wakeup, s: O:4:"User":1:{s:4:"name";s:4:"Leon";}
User Object
(
[name] => Leon
[id] => 4db1b17640da1
)
User Object
(
[name] => Leon
[id] => 4db1b17640dbc
)
*/
// __set_state
/*
This static method is called for classes exported by var_export() since PHP 5.1.0.
The only parameter of this method is an array containing exported properties in the form array('property' => value, ...).
*/
class A {
public $var1;
public $var2;
public static function __set_state($an_array) { // As of PHP 5.1.0
//$an_array打印出来是数组,而不是调用时传递的对象
print_r($an_array);
$obj = new A;
$obj->var1 = $an_array['var1'];
$obj->var2 = $an_array['var2'];
return $obj;
}
}
$a = new A;
$a->var1 = 5;
$a->var2 = 'foo';
echo "__set_state:\n";
eval('$b = ' . var_export($a, true) . ';');
// $b = A::__set_state(array(
// 'var1' => 5,
// 'var2' => 'foo',
// ));
var_dump($b);
echo "\n\n";
/**
输出结果如下:
__set_state:
Array
(
[var1] => 5
[var2] => foo
)
object(A)#5 (2) {
["var1"]=>
int(5)
["var2"]=>
string(3) "foo"
}
*/
// __clone
class SubObject {
static $instances = 0;
public $instance;
public function __construct() {
$this->instance = ++self::$instances;
}
public function __clone() {
$this->instance = ++self::$instances;
}
}
class MyCloneable {
public $object1;
public $object2;
function __clone() {
// Force a copy of this->object, otherwise
// it will point to same object.
$this->object1 = clone $this->object1;
}
}
$obj = new MyCloneable();
$obj->object1 = new SubObject();
$obj->object2 = new SubObject();
$obj2 = clone $obj;
print("__clone, Original Object:\n");
print_r($obj);
print("__clone, Cloned Object:\n");
print_r($obj2);
echo "\n\n";
/**
输出结果如下:
__clone, Original Object:
MyCloneable Object
(
[object1] => SubObject Object
(
[instance] => 1
) [object2] => SubObject Object
(
[instance] => 2
))
__clone, Cloned Object:
MyCloneable Object
(
[object1] => SubObject Object
(
[instance] => 3
) [object2] => SubObject Object
(
[instance] => 2
))
*/

PHP在現代編程中仍然是一個強大且廣泛使用的工具,尤其在web開發領域。 1)PHP易用且與數據庫集成無縫,是許多開發者的首選。 2)它支持動態內容生成和麵向對象編程,適合快速創建和維護網站。 3)PHP的性能可以通過緩存和優化數據庫查詢來提升,其廣泛的社區和豐富生態系統使其在當今技術棧中仍具重要地位。

在PHP中,弱引用是通過WeakReference類實現的,不會阻止垃圾回收器回收對象。弱引用適用於緩存系統和事件監聽器等場景,需注意其不能保證對象存活,且垃圾回收可能延遲。

\_\_invoke方法允許對象像函數一樣被調用。 1.定義\_\_invoke方法使對象可被調用。 2.使用$obj(...)語法時,PHP會執行\_\_invoke方法。 3.適用於日誌記錄和計算器等場景,提高代碼靈活性和可讀性。

Fibers在PHP8.1中引入,提升了並發處理能力。 1)Fibers是一種輕量級的並發模型,類似於協程。 2)它們允許開發者手動控制任務的執行流,適合處理I/O密集型任務。 3)使用Fibers可以編寫更高效、響應性更強的代碼。

PHP社區提供了豐富的資源和支持,幫助開發者成長。 1)資源包括官方文檔、教程、博客和開源項目如Laravel和Symfony。 2)支持可以通過StackOverflow、Reddit和Slack頻道獲得。 3)開發動態可以通過關注RFC了解。 4)融入社區可以通過積極參與、貢獻代碼和學習分享來實現。

PHP和Python各有優勢,選擇應基於項目需求。 1.PHP適合web開發,語法簡單,執行效率高。 2.Python適用於數據科學和機器學習,語法簡潔,庫豐富。

PHP不是在消亡,而是在不斷適應和進化。 1)PHP從1994年起經歷多次版本迭代,適應新技術趨勢。 2)目前廣泛應用於電子商務、內容管理系統等領域。 3)PHP8引入JIT編譯器等功能,提升性能和現代化。 4)使用OPcache和遵循PSR-12標準可優化性能和代碼質量。

PHP的未來將通過適應新技術趨勢和引入創新特性來實現:1)適應云計算、容器化和微服務架構,支持Docker和Kubernetes;2)引入JIT編譯器和枚舉類型,提升性能和數據處理效率;3)持續優化性能和推廣最佳實踐。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

WebStorm Mac版
好用的JavaScript開發工具

Dreamweaver Mac版
視覺化網頁開發工具

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

禪工作室 13.0.1
強大的PHP整合開發環境