[英]Object function won't execute in PHP
I'm running Eclipse Indigo with PDT and Xdebugger (all latest versions) and a LAMP server on Ubuntu 11.04. 我正在Ubuntu 11.04上使用PDT和Xdebugger(所有最新版本)和LAMP服务器运行Eclipse Indigo。
While debugging, an object function (see code below) won't execute; 调试时,对象函数(请参见下面的代码)将不会执行; the debugger just defaults - the variables window goes completely blank and it just freezes up. 调试器只是默认设置-变量窗口完全空白,并且冻结。 The page won't load either, it just stays in a loading state. 该页面也不会加载,只是停留在加载状态。
Everything is fine up to the point where I start calling functions on the object. 一切都很好,直到我开始在对象上调用函数为止。
Suggestions? 有什么建议吗?
Here's the code: 这是代码:
<?php
require_once 'user.php';
require_once 'fetcher.php';
require_once 'inscriber.php';
$uname=$_POST['regname'];
$upass=$_POST['regpass'];
$ufirst=$_POST['regfirst'];
$ulast=$_POST['reglast'];
$uemail=$_POST['regemail'];
$uphone=$_POST['regphone'];
$user = new User();
$user->setUsername($uname); // THIS IS WHERE IT FREEZES UP
$user->setPassword($upass);
$user->setFirstname($ufirst);
$user->setLastname($ulast);
$user->setEmail($uemail);
$user->setPhone($uphone);
$inscriber = Inscriber::getInscriberInstance();
$success = $inscriber->inscribeUser($user);
?>
<?php
class User{
private $username;
private $password;
private $userID;
private $firstname;
private $lastname;
private $phone;
private $email;
public function getUsername(){
return $username;
}
public function setUsername($var){
$this->$username = $var;
}
///
public function getPassword(){
return $password;
}
public function setPassword($var){
$this->$password = $var;
}
///
public function getUserID(){
return $userID;
}
public function setUserID($var){
$this->$userID = $var;
}
///
public function getFirstname(){
return $firstname;
}
public function setFirstname($var){
$this->$firstname = $var;
}
///
public function getLastname(){
return $lastname;
}
public function setLastname($var){
$this->$lastname = $var;
}
///
public function getPhone(){
return $phone;
}
public function setPhone($var){
$this->$phone = $var;
}
///
public function getEmail(){
return $email;
}
public function setEmail($var){
$this->$email = $var;
}
}
$this->$username = $var;
This is a "dynamic property". 这是“动态特性”。 PHP tries to replace $username
with the content of the variable. PHP尝试将$username
替换$username
变量的内容。 The variable doesn't exists, so the resulting $this-> = $var
just fails 该变量不存在,因此生成的$this-> = $var
只会失败
$this->username = $var;
(non-static) properties are always called without the $
. (非静态)属性始终不带$
调用。
Additional in the getters you are using local variables 在使用本地变量的getter中的附加功能
public function getUsername(){
return $username;
}
Don't know, why you (at least try to) use properties in setters, but use local variables in getters 不知道,为什么(至少尝试)在setter中使用属性,而在getter中使用局部变量
public function getUsername(){
return $this->username;
}
Sidenote: "objects functions" are called "methods" 旁注:“对象函数”称为“方法”
Your syntax is not quite correct (in this case). 您的语法不太正确(在这种情况下)。 You want to do this: 您想这样做:
//...
public function getUsername(){
return $this->username; //added $this->...
}
public function setUsername($var){
$this->username = $var; //no $-sign before 'username'
}
//...
This goes for all the other functions, too. 其他所有功能也是如此。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.