超越PHP PHP动态 | 经典文章 | CLASS | 相关下载 | 常见问题 | FORUM | WIKI | 在线手册
Site search:    
<数组资源>
Last updated: Fri, 22 Jun 2007

对象

对象初始化

要初始化一个对象,用 new 语句将对象实例到一个变量中。

<?php
class foo
{
   function
do_foo()
   {
       echo
"Doing foo.";
   }
}

$bar = new foo;
$bar->do_foo();
?>

完整的讨论见类与对象一章。

转换为对象

如果将一个对象转换成对象,它将不会有任何变化。如果其它任何类型的值被转换成对象,内置类 stdClass 的一个实例将被建立。如果该值为 NULL,则新的实例为空。对于任何其它的值,名为 scalar 的成员变量将包含该值。

<?php
$obj
= (object) 'ciao';
echo
$obj->scalar// outputs 'ciao'
?>




add a note add a note User Contributed Notes
对象
php at electricsurfer dot com
23-May-2003 12:25
Here's an example on operator precedence between ->  and []
& what happens with $object->$member[$array_element]

<?
class c
{
var $a = array('a'=>'aa','b'=>'ab');
var $b = 'c';

function show()
{
echo $this->a['a']; // -> 1st
echo $this->a['b']; // outputs 'ab'

$a = 'a';
$b = 'b';

echo $this->$a[$a]; // [] 1st, not what I expected
echo $this->$a[$b]; // does NOT output 'ab'

$this_a =& $this->$a; // work-around

echo $this_a[$a]; // no question
echo $this_a[$b];

$a_arr = array('a'=>'b');

echo $this->$a_arr[$a]; // [] 1st => outputs 'c'
}
}
$c = new c();
$c->show();
?>

<数组资源>
 Last updated: Fri, 22 Jun 2007
view source | feedback | send page | sitemap | aboutus   
Copyright ® 2002-2003 PHPE.NET. All rights reserved
Last updated:2002-11-22