2017年8月26日 星期六

PHP的get_called_class()

在以下例子中,我們可以使用get_called_class()來知道$result是用哪個Class的_test()函數生成的
class B extends A
{
 public static function _test(){
  return 'B_result';
 }
}

class C extends A
{
 public static function _test(){
  return 'C_result';
 }
}

class A
{
 public static function test(){
  $result = static::_test();
  echo get_called_class();
  echo $result;
 }
}

$sClass = 'C';
$sClass::test();
output:
C
$result:C_result

雖然看似簡單,但是如果_test()算出來的結果不是那麼的規律,而且$sClass這邊又不是寫的這麼直觀。可以用get_called_class()來找到A::test()的static::_test()是呼叫哪個Class的_test()

參考資料:
https://stackoverflow.com/questions/506705/how-can-i-get-the-classname-from-a-static-call-in-an-extended-php-class  How can I get the classname from a static call in an extended PHP class?



2017年8月16日 星期三

laravel 4.2 心得

安裝PHP memcached(非memcache)

錯誤:
Class 'Memcached' not found
# yum install php-pecl-memcached

PHP 5.4升級5.5

環境:centOS 7.1
http://www.techoism.com/how-to-upgrade-php-version-5-4-to-5-5-on-centosrhel/
移除舊的php
# yum remove php-common
安裝PHP 5.5和其套件
# yum -y install php55w php55w-opcache
# yum -y install php55w-xml php55w-mcrypt php55w-gd php55w-devel php55w-mysql php55w-intl php55w-mbstring
安裝其餘必要擴展
# yum install php55w-fpm
# yum install php55w-pecl-memcache
# yum install php55w-pecl-memcached
重啟php-fpm
# service php-fpm restart

安裝redis

https://www.linode.com/docs/databases/redis/install-and-configure-redis-on-centos-7
# yum install redis
啟動redis
# systemctl start redis
開機自動啟動redis(可選)
# systemctl enable redis
測試redis是否安裝成功
# redis-cli ping
PONG

安裝supervisor

# yum install python-setuptools
# yum install supervisor
配置
# vim /etc/supervisord.conf
[include]
files = supervisord.d/*.ini
files = supervisord.d/*.conf
# vim /etc/supervisord.d/your.conf
[program:ga2-main]
command = php /laravel_path/artisan queue:listen ga2-main
process_name = %(program_name)s_%(process_num)s
numprocs = 1
autostart=true
autorestart=true
stdout_logfile=/var/log/supervisor/ga2-main.log

啟動supervisor
# service supervisord start
停用supervisor
# service supervisord stop

檢查是否有跑
# ps aux | grep supervisord
root     14983  0.3  0.2 223064  2696 ?        Ss   12:25   0:01 /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf
# ps aux | grep ga2-main
root     16471  0.1  2.1 411032 22236 ?        S    12:26   0:00 php /laravel_path/artisan queue:listen ga2-main

勿用pip或 easy_install 安裝,測試安裝後啟動不了supervisor
移除用pip或easy_install安裝的supervisor
# pip uninstall supervisor
列出systemd的unit files
# systemctl list-unit-files