例子,php对象实例化,如下图:
代码:
复制代码 代码示例:
<?php
/*实例化对象
* $对象名=new 类名称()
*/
class Person
{
//成员属性
var $name;
var $age;
var $sex;
var $phone;
//成员方法
function say()
{
echo"我在说话";
}
function run()
{
echo"我在走路";
}
function eat()
{
echo "我在吃饭";
}
function work()
{
echo "我在工作";
}
function study()
{
}
}
class Phone
{
var $manufacturers;
var $color;
var $size;
function call()
{
}
function answerCall()
{
}
function sendMessage($person,$message)
{
$person->phone->recieveMessage($message);
}
function recieveMessage($message)
{
echo "接收的信息是:".$message."<br>";
}
function playMusic()
{
}
}
$p1=new Person();
$p2=new Person();
$p3=new Person();
$phone1=new Phone();
$phone2=new Phone();
$phone3=new Phone();
$p1->name="张三";
$p2->name="李四";
$p3->name="王五";
$phone1->manufacturers="nokia";
$phone2->manufacturers="semmens";
$phone3->manufacturers="samsung";
$p1->phone=$phone1;
$p2->phone=$phone2;
$p3->phone=$phone3;
$p1->phone->sendMessage($p2,"hello");
/* $p1->say();
$p2->eat();
$p3->run(); */
?>