class A
{
public function __construct()
{
echo 'Class A living' . PHP_EOL;
}
public function __destruct()
{
echo 'Class A dead' . PHP_EOL;
}
}
class B
{
public function __construct()
{
echo 'Class B living' . PHP_EOL;
}
public function __destruct()
{
echo 'Class B dead' . PHP_EOL;
}
}
$a = new A();
$b = new B();
Class A living
Class B living
Class B dead
Class A dead
構造方法:類例項化時,呼叫的方法
析構方法:物件銷燬時,呼叫的方法
Tip:類的例項是以堆疊的形式放在記憶體中,最後銷燬的時候,按照先進後出的順序。所以解構函式呼叫的順序也是先進後出。
class A
{
protected $protected = 'protected';
private $private = 'private';
protected function protectedMethod()
{
return 'Hello protected';
}
private function privateMethod()
{
return 'Hello private';
}
}
方法一
class A
{
public function __get($name)
{
return $this->$name;
}
public function __call($name, $arguments)
{
return $this->$name(...$arguments);
}
}
$test = new A();
$test->protected;
$test->private;
$test->protectedMethod();
$test->privateMethod();
方法二
$test = function () {
echo $this->private;
echo $this->privateMethod();
};
$test = $test->bindTo(new A(), 'A');
$test();
關鍵字 join
有時候我們為了得到完整的結果,我們需要從兩個或者多個表中獲取我們的結果,需要用到 join 關鍵字。
- inner join
- left join
- right join
- full join
假如我們們有兩張表
- customers id name …
- orders id customer_id amount …
-
列出所有顧客的訂單
select * from customers inner join orders on customers.id = orders.customer_id
-
列出所有顧客以及他們的訂單(如果有的話)
select * from customers left join orders on customers.id = orders.customer_id
-
列出所有訂單,以及購買它們的顧客(如果有的話)
select * from customers right join orders on customers.id = orders.customer_id
-
列出所有顧客,所有訂單
select * from customers full join orders on customers.id = orders.customer_id
是不是都非常容易,積少成多嘛!
本作品採用《CC 協議》,轉載必須註明作者和本文連結