在PHP中把對象當數組使用_PHP教程

來源:互聯網
上載者:User
我們瞭解,JAVASCRIPT中,對象的屬性、方法,是可以用數組的模式來訪問的。但通常情況下是不可能的。

為什麼要這麼做?這是因為,通過這一方式,可以更加方便地操作對象,我們可以定義一個類。而不是定義一個Key Value數組。自然,如果我們還有其它的辦法,一種最簡單的,就是強制轉換成數組。但,這樣會失去對象中原有的方法。

不過,SPL中的ArrayObject可以協助我們用數組模式訪問屬性。但方法仍不能實現。

ArrayObject類結構如下(部分方法是在php5,1或php5.2時才加上的):

Php代碼
ArrayObject implements IteratorAggregate , Traversable , ArrayAccess , Serializable , Countable {
/* 常量 */
const integer STD_PROP_LIST = 1 ;
const integer ARRAY_AS_PROPS = 2 ;
/* 方法 */
__construct ([ mixed $input [, int $flags [, string $iterator_class ]]] )
void append ( mixed $value )
void asort ( void )
int count ( void )
array exchangeArray ( mixed $input )
array getArrayCopy ( void )
int getFlags ( void )
ArrayIterator getIterator ( void )
int getIteratorClass ( void )
void ksort ( void )
void natcasesort ( void )
void natsort ( void )
bool offsetExists ( mixed $index )
mixed offsetGet ( mixed $index )
void offsetSet ( mixed $index , mixed $newval )
void offsetUnset ( mixed $index )
public void serialize ( void )
void setFlags ( int $flags )
void setIteratorClass ( string $iterator_class )
void uasort ( callback $cmp_function )
void uksort ( callback $cmp_function )
public void unserialize ( string $serialized )
}

ArrayObject implements IteratorAggregate , Traversable , ArrayAccess , Serializable , Countable {
/* 常量 */
const integer STD_PROP_LIST = 1 ;
const integer ARRAY_AS_PROPS = 2 ;
/* 方法 */
__construct ([ mixed $input [, int $flags [, string $iterator_class ]]] )
void append ( mixed $value )
void asort ( void )
int count ( void )
array exchangeArray ( mixed $input )
array getArrayCopy ( void )
int getFlags ( void )
ArrayIterator getIterator ( void )
int getIteratorClass ( void )
void ksort ( void )
void natcasesort ( void )
void natsort ( void )
bool offsetExists ( mixed $index )
mixed offsetGet ( mixed $index )
void offsetSet ( mixed $index , mixed $newval )
void offsetUnset ( mixed $index )
public void serialize ( void )
void setFlags ( int $flags )
void setIteratorClass ( string $iterator_class )
void uasort ( callback $cmp_function )
void uksort ( callback $cmp_function )
public void unserialize ( string $serialized )
}

其中:我們為什麼可以用 $obj[name] 直接存取到 $obj->name呢? 主要是上面的方法中的三個方法:

offsetGet 支援$obj[name] 讀的方式

offsetSet 支援$obj[name] 寫的方式

但foreach則是該類對ArrayAccess的函數Current等的預設實現。

看一個例子代碼:

Php代碼
class test extends ArrayObject{
public $name;
private $age = 21;
public function show(){
print_r(get_object_vars($this));
}
}
class test1{
public $name;
private $age = 21;
public function show(){
print_r(get_object_vars($this));
}
}
$obj=new test();
//使用數組方式讀寫屬性
$obj[name]=hello;
$obj[nick]=mockArray;
echo $obj[nick],
;
var_dump($obj[show]);//檢測是否可以存取方法:
print_r($obj);//輸出對象
$obj->show();//調用方法
$arr=(array)$obj; //強制轉換成數組。
print_r($arr);
//$arr->show(); 此行將出錯,因為,原有方法全部丟失。
$obj1=new test1(); //建立普通對象
$arr1=(array)$obj1; //強制轉換成數組。
print_r($arr1); //隱私完全暴光

class test extends ArrayObject{
public $name;
private $age = 21;
public function show(){
print_r(get_object_vars($this));
}
}
class test1{
public $name;
private $age = 21;
public function show(){
print_r(get_object_vars($this));
}
}
$obj=new test();
//使用數組方式讀寫屬性
$obj[name]=hello;
$obj[nick]=mockArray;
echo $obj[nick],
;
var_dump($obj[show]);//檢測是否可以存取方法:
print_r($obj);//輸出對象
$obj->show();//調用方法
$arr=(array)$obj; //強制轉換成數組。
print_r($arr);
//$arr->show(); 此行將出錯,因為,原有方法全部丟失。
$obj1=new test1(); //建立普通對象
$arr1=(array)$obj1; //強制轉換成數組。
print_r($arr1); //隱私完全暴光

這段代碼會輸出:

mockArray
NULL
test Object
(
[name] => hello
[nick] => mockArray
)
Array
(
[name] => hello
[nick] => mockArray
)
Array
(
[name] => hello
[nick] => mockArray
)
Array
(
[name] =>
[ test1 age] => 21
)

可以看出,完全可以使用數組模式訪問屬性,但不能訪問到方法(成員函數)。

強制轉換後,即是數組對象,再無成員函數了。

當然 offsetGet offsetSet 這兩個方法,也可以根據我們的需要進一步改寫。為什嗎?因為,如果有一些十分變態的需求之時,肯定有用。比如,我們要將三個數組用引用的方式封裝到一個對象中,當成一個數組來訪問。這時,就要重寫這兩個函數。當然,同時也要重寫ArrayAccess介面中對應的函數。

再有,能夠訪問到的均是公有屬性。如果是私人的,則訪問不到的。即便是強制轉換成數組,也是一樣。但如果沒有繼承ArrayObject,則就不同了。這樣的類,一旦強制轉換成數組,其隱私(私人的屬性)也就被暴光了。

不過我們可以看出,私人屬性轉換成數組以後,未保留原有屬性名稱。而是使用了:某個不可列印字元+類名+不可列印字元+屬性名稱的形式。這個不可列印字元ASCII是多少沒有查,你要有興趣可以查一下!

http://www.bkjia.com/PHPjc/478766.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/478766.htmlTechArticle我們瞭解,JAVASCRIPT中,對象的屬性、方法,是可以用數組的模式來訪問的。但通常情況下是不可能的。 為什麼要這麼做?這是因為,通過...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.