php public 函数在 class 之外不起作用
php public function is not working outside of the class
我创建了一个简单的 class、变量和 public 函数。当我尝试启动一个对象并从 class 外部的 class 调用 public 函数时,我看不到任何输出。
<?php
error_reporting(E_ALL);
ini_set("error_reporting", 1);
class Game {
var name;
public function show_game() {
echo $this->name;
}
}
$game = new Game;
$game->name = "testing game";
$game->show_game();
?>
据我了解,该函数应该回显 测试游戏。但是当我加载页面时我没有看到任何输出。
var name;
语法无效,更改为:
var $name;
您似乎只是忘记了 PHP 变量中的 $
。 var name
应该是 var $name
:
<?php
class Game {
var $name;
public function show_game() {
echo $this->name; // Returns "testing game"
}
}
$game = new Game;
$game->name = "testing game";
$game->show_game();
?>
希望对您有所帮助! :)
我创建了一个简单的 class、变量和 public 函数。当我尝试启动一个对象并从 class 外部的 class 调用 public 函数时,我看不到任何输出。
<?php
error_reporting(E_ALL);
ini_set("error_reporting", 1);
class Game {
var name;
public function show_game() {
echo $this->name;
}
}
$game = new Game;
$game->name = "testing game";
$game->show_game();
?>
据我了解,该函数应该回显 测试游戏。但是当我加载页面时我没有看到任何输出。
var name;
语法无效,更改为:
var $name;
您似乎只是忘记了 PHP 变量中的 $
。 var name
应该是 var $name
:
<?php
class Game {
var $name;
public function show_game() {
echo $this->name; // Returns "testing game"
}
}
$game = new Game;
$game->name = "testing game";
$game->show_game();
?>
希望对您有所帮助! :)