The class "MyClass" was initiated! Catchable fatal error: Object of class MyClass could not be converted to string in /Applications/MAMP/htdocs/CYOProject/ooptest/test.php on line 40
<?php class MyClass { public $prop1 = "I'm a class property!";
public function __construct() { echo 'The class "' . __CLASS__ . '" was initiated!<br />'; }
public function __destruct() { echo 'The class "' . __CLASS__ . '" was destroyed.<br />'; }
public function __toString() { echo "Using the toString method: "; return $this->getProperty(); }
public function setProperty($newval) { $this->prop1 = $newval; }
public function getProperty() { return $this->prop1 . "<br />"; } }
class MyOtherClass extends MyClass { public function __construct() { parent::__construct(); // Call the parent class's constructor echo "A new constructor in " . __CLASS__ . ".<br />"; }
public function newMethod() { echo "From a new method in " . __CLASS__ . ".<br />"; } }
// Create a objects $newobj = new MyOtherClass;
// Output the object as a string echo $newobj->newMethod();
// Use a method from the parent class echo $newobj->getProperty();
The class "MyClass" was initiated! A new constructor in MyOtherClass. From a new method in MyOtherClass. I'm a class property! The class "MyClass" was destroyed.
七、替屬性和方法加上可視性 Assigning the Visibility of Properties and Methods
加入可視性(Visibility),可以決定能不能從物件外面控制物件的方法和屬性。
可視性有種:public、protected和private。
替屬性和方法加上可視性,是為了增加對物件的控制。 For added control over objects, methods and properties are assigned visibility.
class MyOtherClass extends MyClass { public function __construct() { parent::__construct(); // Call the parent class's constructor echo "A new constructor in " . __CLASS__ . ".<br />"; }
public function newMethod() { echo "From a new method in " . __CLASS__ . ".<br />"; } }
// Create a objects $newobj = new MyOtherClass;
// Use a method from the parent class echo $newobj->getProperty();
執行這段程式碼之後,會出現以下 Call to protected method 錯誤:
1 2 3
The class "MyClass" was initiated! A new constructor in MyOtherClass. Fatal error: Call to protected method MyClass::getProperty() from context '' in /Applications/MAMP/htdocs/CYOProject/ooptest/test.php line 55
接下來,在子類別 MyOtherClass 中新增一個 public 方法來調用 getProperty() :
class MyOtherClass extends MyClass { public function __construct() { parent::__construct(); // Call the parent class's constructor echo "A new constructor in " . __CLASS__ . ".<br />"; }
public function newMethod() { echo "From a new method in " . __CLASS__ . ".<br />"; }
public function callProtected() { return $this->getProperty(); } }
// Create a objects $newobj = new MyOtherClass;
// Call the protected method from within a public method echo $newobj->callProtected();
由結果可見,子類別可以調用父類別可性度為 protected 的方法:
1 2 3 4
The class "MyClass" was initiated! A new constructor in MyOtherClass. I'm a class property! The class "MyClass" was destroyed.
class MyOtherClass extends MyClass { public function __construct() { parent::__construct(); // Call the parent class's constructor echo "A new constructor in " . __CLASS__ . ".<br />"; }
public function newMethod() { echo "From a new method in " . __CLASS__ . ".<br />"; }
public function callProtected() { return $this->getProperty(); } }
// Create a objects $newobj = new MyOtherClass;
// Call the protected method from within a public method echo $newobj->callProtected();
重新載入瀏覽器,會因為 private 調用權限不足而得到錯誤訊息:
1 2 3
The class "MyClass" was initiated! A new constructor in MyOtherClass. Fatal error: Call to private method MyClass::getProperty() from context 'MyOtherClass' in /Applications/MAMP/htdocs/CYOProject/ooptest/test.php line 49
<?php class MyClass { public $prop1 = "I'm a class property!"; public static $count = 0;
public function __construct() { echo 'The class "' . __CLASS__ . '" was initiated!<br />'; }
public function __destruct() { echo 'The class "' . __CLASS__ . '" was destroyed.<br />'; }
public function __toString() { echo "Using the toString method: "; return $this->getProperty(); }
public function setProperty($newval) { $this->prop1 = $newval; }
private function getProperty() { return $this->prop1 . "<br />"; }
public static function plusOne() { return "The count is " . ++self::$count . ".<br />"; } }
class MyOtherClass extends MyClass { public function __construct() { parent::__construct(); // Call the parent class's constructor echo "A new constructor in " . __CLASS__ . ".<br />"; }
public function newMethod() { echo "From a new method in " . __CLASS__ . ".<br />"; }
public function callProtected() { return $this->getProperty(); } }
do { // Call plusOne without instantiating MyClass echo MyClass::plusOne(); } while (MyClass::$count < 10);
小筆記
當使用範圍解析運算子存取 static 屬性時,記得要在屬性名稱前面加上錢字號 $
輸出結果為:
1 2 3 4 5 6 7 8 9 10
The count is 1. The count is 2. The count is 3. The count is 4. The count is 5. The count is 6. The count is 7. The count is 8. The count is 9. The count is 10.
<?php class MyClass { const CLASS_NAME = 'MyClass';
public $prop1 = "I'm a class property!"; public static $count = 0;
public function __construct() { echo 'The class "' . __CLASS__ . '" was initiated!<br />'; }
public function __destruct() { echo 'The class "' . __CLASS__ . '" was destroyed.<br />'; }
public function __toString() { echo "Using the toString method: "; return $this->getProperty(); }
public function setProperty($newval) { $this->prop1 = $newval; }
private function getProperty() { return $this->prop1 . "<br />"; }
public static function plusOne() { return "The count is " . ++self::$count . ".<br />"; } }
class MyOtherClass extends MyClass { public function __construct() { parent::__construct(); // Call the parent class's constructor echo "A new constructor in " . __CLASS__ . ".<br />"; }
public function newMethod() { echo "From a new method in " . __CLASS__ . ".<br />"; }
public function callProtected() { return $this->getProperty(); } }