魔术方法callStatic应用场景:后期静态绑定、链式调用

魔术方法是PHP的重载机制,当调用类中不存在或不可访问的静态方法是,将调用类中的__callStatic方法。
基于这种重载机制,就有很多使用技巧可以利用,下边举了两个例子。

后期静态绑定

laravel eloquent orm的model抽象类中也有后期静态绑定的使用,最常用的比如$table变量。
当然不局限于orm,很多其他组件当中也有使用,开发架构中也可以使用。

abstract class Model
{
	public static function getTable()
	{
		return __FUNCTION__ . ':' . static::$table;
	}

	private function find(...$args)
	{
		return __FUNCTION__ . ':' . implode('', $args);
	}

	public static function __callStatic($method, $args)
	{
		return static::$method(...$args);
	}
}

class UserModel extends Model
{
	protected static $table = 'users';
}

echo UserModel::getTable(); // 一般后期静态绑定使用方式

echo "\n";

echo UserModel::find('a', 'b'); // 结合魔术方法的后期静态绑定使用方式

/*

output:

getTable:users
find:ab

*/

链式调用

laravel eloquent orm的构造查询器也是类似思路实现的,但是要复杂很多。

abstract class Model
{
	public static function getTable()
	{
		echo __FUNCTION__ . ':' . static::$table;
		return (new static);
	}

	private function find(...$args)
	{
		echo __FUNCTION__ . ':' . implode('', $args);
		return (new static);
	}

	public static function __callStatic($method, $args)
	{
		return static::$method(...$args);
	}

	public function __call($method, $args) 
	{
		return $this->$method(...$args);
	}
}

class UserModel extends Model
{
	protected static $table = 'users';
}

UserModel::getTable()->find('a', 'b')->find('c', 'd');

/*

output:

getTable:usersfind:abfind:cd

*/

参考链接

https://www.php.net/manual/zh/language.oop5.late-static-bindings.php

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值