简体   繁体   中英

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.

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. The variable doesn't exists, so the resulting $this-> = $var just fails

$this->username = $var;

(non-static) properties are always called without the $ .

Additional in the getters you are using local variables

public function getUsername(){
    return $username;
}

Don't know, why you (at least try to) use properties in setters, but use local variables in getters

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM