<?php header('content-type:text/html;charset=utf-8'); class Test { static $p = 5; const USERNAME = '******'; const PASSWORD = 123; function fun1() { echo self::USERNAME, '<br/><br/>'; echo Test::PASSWORD, '<br/><br/>'; echo '-----------------------------<br/><br/>'; } } $t = new Test(); $t->fun1(); echo Test::$p, '<br/><br/>'; echo Test::USERNAME, '<br/><br/>';
$std4->username = '******'; //数组的成员值可以为对象 $arr = array($std1, $std2, $std3, $std4); foreach ($arr as $obj) { echo $obj->userId, '--', $obj->username, '<br/><br/>'; } //对象的属性值或方法的返回值可以为数组 class Test { public $users = array('Tom', 'John', 'Rose'); function fun1() { $std1 = new stdClass(); $std1->userId = 5; $std1->username = '******'; $std2 = new stdClass(); $std2->userId = 9; $std2->username = '******'; $std3 = new stdClass(); $std3->userId = 22; $std3->username = '******'; $std4 = new stdClass(); $std4->userId = 36; $std4->username = '******'; return array($std1, $std2, $std3, $std4); } } $t = new Test(); echo $t->users[1], '<br/><br/>'; echo $t->fun1()[2]->username, '<br/><br/>';
public function fun2() { echo '我是从普通方法中来的' . Test::fun1(), '<br/><br/>'; }