有人可以解释我并为一起工作的静态和非静态类做一些例子吗?
喜欢:
App::MySQL()->insert();
App::User()->name;
我在网上找不到任何东西。
谢谢
<?php
class App {
public static function MySQL() {
return new MySQL();
}
}
class MySQL {
public function insert() {
echo "inserting some stuff\n";
}
}
App::MySQL()->insert();
给
ivo@atmycomputer ~$ php test.php
inserting some stuff
ivo@atmycomputer ~$
The first part (e.g. App::MySQL(), App::User()) is calling a static method on a class in order to return a singleton instance (meaning if an instance of that class exists return it, else create a new one and return it), this makes sure that you have at most one instance of that class. Then you call its methods or get its properties (the second part: ->insert(), ->name)