當我運行下面的代碼時我得到了錯誤echo $attribute;
錯誤代碼:「可捕捉的致命錯誤:類SomeShape的對象無法轉換爲字符串」: 這個代碼? 謝謝。我想要初始化屬性而不創建對象
<?php
class Shape
{
static public $width;
static public $height;
}
class SomeShape extends Shape
{
public function __construct()
{
$test=self::$width * self::$height;
echo $test;
return $test;
}
}
class SomeShape1 extends Shape
{
public function __construct()
{
return self::$height * self::$width * .5;
}
}
Shape::$width=60;
Shape::$height=5;
echo Shape::$height;
$attribute = new SomeShape;
echo $attribute;
$attribute1 = new SomeShape1;
echo $attribute1;
?>
這可能幫助你http://stackoverflow.com/questions/829823/can-you-create-class-properties-dynamically-in-php –
哪條線路會導致錯誤? – Brady