最簡單的使用方法值的對象。
class MSG_HEAD
{
var $c, $size, $headcode;
}
class GET_INFO
{
var $h, $Type, $Port, $Name, $Code;
function __construct() {
$this->h = new MSG_HEAD();
}
}
function Example (GET_INFO $msg)
{
print ($msg->Name);
print ($msg->Code);
}
使用getter和setter方法這是一個比較先進的,但應該允許它更像一個結構
class MSG_HEAD
{
protected $c;
protected $size;
protected $headcode;
function __get($prop) {
return $this->$prop;
}
function __set($prop, $val) {
$this->$prop = $val;
}
}
class GET_INFO
{
protected $MSG_HEAD;
protected $Type;
protected $Port;
protected $Name;
protected $Code;
function __construct() {
$this->MSG_HEAD = new MSG_HEAD();
}
function __get($prop) {
return $this->$prop;
}
function __set($prop, $val) {
$this->$prop = $val;
}
}
function Example (GET_INFO $msg)
{
print ($msg->Name);
print ($msg->Code);
}
如果你想確保滿足h實際上是類,你可以使用getter和setter方法,以確保您始終創建的是該類的新實例。 – Dimentox 2011-03-05 22:47:32