php setter和getter不工作
我的php getter和setter似乎工作不正常。这是我的密码。文件test.php包含以下内容php setter和getter不工作,php,oop,get,set,Php,Oop,Get,Set,我的php getter和setter似乎工作不正常。这是我的密码。文件test.php包含以下内容 class test{ private static $instance; private $name=null; public static function getInstance(){ if(!isset(test::$instance) ) { test::$instance = new test(
class test{
private static $instance;
private $name=null;
public static function getInstance(){
if(!isset(test::$instance) ) {
test::$instance = new test();
}
return test::$instance;
}
public function setName($n){
$this->name=$n;
}
public function getName(){
return $this->name;
}
php有一个表单,要求用户输入自己的名字。当他们单击submit时,我有这样的代码来设置他们在测试中输入的变量名
$test=test::getInstance();
$test->setName("My PHP Test");
print "The name is " . $test->getName() . "<br />;
名称已打印
我添加了include('test.php')代码>到每个文件,但这没有帮助
我的代码中有错误吗?一般来说,这也是正确的方法吗?如果不是,是什么
任何反馈都将不胜感激。谢谢。对象不会自动持久化,您需要将数据存储在test2.php中,然后使用数据库、表单或cookie在test3.php中检索数据
我添加了include('test.php');到每个文件,但这没有帮助
可能是因为您正在创建一个新的test实例,该实例将覆盖test.php中的实例。对象不会自动持久化,因此您需要将数据存储在test2.php中,并使用数据库、表单或cookie在test3.php中检索数据
我添加了include('test.php');到每个文件,但这没有帮助
可能是因为您正在创建一个新的test实例,该实例将覆盖test.php中的实例。对象不会自动持久化,因此您需要将数据存储在test2.php中,并使用数据库、表单或cookie在test3.php中检索数据
我添加了include('test.php');到每个文件,但这没有帮助
可能是因为您正在创建一个新的test实例,该实例将覆盖test.php中的实例。对象不会自动持久化,因此您需要将数据存储在test2.php中,并使用数据库、表单或cookie在test3.php中检索数据
我添加了include('test.php');到每个文件,但这没有帮助
可能是因为您正在创建一个新的test实例,该实例覆盖了test中的实例。php也可以使用会话,但这也是spot-onCould-use会话,但这也是spot-onCould-use会话,但这也是spot-onCould-use会话,但这是spot-on-on-use会话
$test=test::getInstance();
print "The name is " . $test->getName() . "<br />;