php中定義類

hopegrace發表於2012-10-19

<?php

class Person{ //定義了一個Person

public $name; //定義屬性name

public $age; //定義屬性age

function  __construct($name,$age){

    $this->name =  $name;

    $this->age = $age;

}

function say( ) //定義方法say( )

{ echo “my name is “.$this->name.”<br>”;  }

}

$zhangsan=new Person(); //生成物件zhangsan

$zhangsan->name=”zhangsan”; //給物件中的name賦值

$zhangsan->age=26; //給物件中的age賦值

$zhangsan->say(); //呼叫方法say()

echo “age is “.$zhangsan->age; //輸出age

?>

注:英文句號(.)可將字串連線合併成新的字串


相關文章