月曜日, 2月 13, 2012

"Overload"? or hooking get/set in PHP

In PHP, there are a set of functions that hooks getting and setting private member variables.  When the variable is set, __set() method will be called.  When the variable is accessed, __get() method is called.  In PHP's terminology, hooking variable definition and retrieval are called overloading. 

class HookTest
{
private $a=1;

public function __get($name)
{
  echo "$name is being retrieved<br />";
  return $this->$name;
}

public function __set($name,$value)
{
  echo "$name is being set<br />";
  $this->$name=3;
}
}

echo "<pre>";

$test=new HookTest;
$test->a=2;
echo $test->a;

PHP: 定数を扱う

プロジェクトごとの定数を扱うクラス Config\Constants の紹介です。 <?php namespace Config; class Constants {     public const DB_USER = "linguist...