PHP类get方法始终返回空
这让我快发疯了。。。我似乎不明白为什么我的PHP类get方法始终返回空,php,function,class,oop,Php,Function,Class,Oop,这让我快发疯了。。。我似乎不明白为什么我的getJava函数不起作用: <?php class Cup { public $java = null; public function __construct() { $this->java = "four"; } function getJava() { return $this->java; } } $asset = new Cup; echo
getJava
函数不起作用:
<?php
class Cup
{
public $java = null;
public function __construct() {
$this->java = "four";
}
function getJava() {
return $this->java;
}
}
$asset = new Cup;
echo "Item: " . $asset->java . "<br />";
echo "Item: " . $asset->getJava . "<br />";
// Debug
echo "<pre>";
var_dump($asset);
echo "</pre>";
?>
因为您试图访问未定义的属性
getJava
,而不是getJava()
方法。请尝试以下操作:
echo“项目:”$资产->获取Java()。“
”;
您正在调用一个方法:$asset->getJava()
$asset->getJava
对于直接访问实例$asset
@user3583258的公共属性getJava
是有效的-感觉愚蠢是大自然提醒我们在未来认识到同样错误的方式:)
Item: four
Item:
object(Cup)#1 (1) {
["java"]=>
string(4) "four"
}
echo "Item: " . $asset->getJava() . "<br />";