Skip to content

Instantly share code, notes, and snippets.

@haxianhe
Last active July 12, 2019 06:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haxianhe/8f4f779ca1d702d86d51f3fac735e5f8 to your computer and use it in GitHub Desktop.
Save haxianhe/8f4f779ca1d702d86d51f3fac735e5f8 to your computer and use it in GitHub Desktop.
PHP 用trait实现的单例
<?php
trait Service_Data_Base_Singleton
{
private static $singleton;
private function __construct(){}
public static function getInstance() {
if( !(self::$singleton instanceof self) ) {
self::$singleton = new self();
}
return self::$singleton;
}
}
<?php
class Model_Rpc_Merchant extends Sflib_RpcBase
{
use Service_Data_Base_Singleton;
public function getStockAlarmInfo( $shop_id ) {
$params = [
'shop_id' => $shop_id
];
$path = '/api/merchant/api/checkskustock';
return $this->_get( $path, $params );
}
}
<?php
$sku_info = Model_Rpc_Merchant::getInstance()->getSkuInfoWithShop( $shop_id, $sku_params );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment