標籤:eth else 效果 get image 分享 使用 屬性 基本
<?php//類的概念/* * 一個類包含自己的屬性和函數 * * 屬性:屬於類自己的常量和變數 * * 方法:就是函數 * * 類是一類事物的抽象 *///例子://車就是一種抽象class Car{ //車的基本屬性:有輪子,有顏色,可以拉貨(人) public $color = ‘red‘;//預設紅色 public $wheel_size = 4;//預設4個 //方法:拉貨預設 public function pull_some_thing($something = ‘貨‘){ echo "我是一輛 $this->color 色的,有 $this->wheel_size 個輪子的車,我正在拉 $something "; }}$qiche = new Car();$qiche->color = ‘黑‘;$qiche->wheel_size = 12;$qiche->pull_some_thing(‘人‘);echo ‘<hr/>‘;//例子2:class A{ function foo() { if (isset($this)) { echo ‘$this is defined (‘; echo get_class($this);//獲得這個$this 代表的類 echo ")\n"; } else { echo "\$this is not defined.\n"; } }}class B{ function bar() { // Note: the next line will issue a warning if E_STRICT is enabled. A::foo();//在b類下調用A類的foo方法,獲得的$this 是B類 }}$a = new A();$a->foo();@A::foo();//直接在類外調用A類的foo方法不能擷取到A類(就是說不能在類外使用$this指代目標類)//$this->foo();//會報錯$b = new B();@$b->bar();// Note: the next line will issue a warning if E_STRICT is enabled.@B::bar();echo ‘<br/>‘;
執行效果
php學習筆記(一)————php類的概念