Use more than one -> in the same PHP code line

Asked

Viewed 28 times

0

I made my transition from Java to PHP and recently bumped into something I used to do in Java but am not able to do in php.

The following code creates an object of type "User"

public function validateUser() {
    //Check if information entered by user is valid
    $user = new User($_POST);
    new UserControl($user);
}

I need to do a validation if Cpf is numeric before proceeding with the validation with the formula. In Java I would use something like:

int cpf = this.getUser().getUserCpf();

The php code looks like this:

class UserControl {
private $user;

public function __construct($user) {
   $this->setUser($user);
}


public function validateIntegers() {
  $cpf = $this->getUser()->getUserCpf();
  is_numeric($cpf);
}

But the same logic does not apply. Is there any other way to use this object orientation in PHP? Or is it not possible to do something like this?

  • 1
  • I couldn’t understand exactly where the problem is. $cpf comes void?

  • Exactly. $Cpf is normal after creating the User object, but when I pass $user as a parameter to build the Usercontrol class it is null.

  • Put the code of getUser()

  • public Function getUser() { Return $this->user; }

  • I have to cast this as Return (Object) $this->user;?

  • No need to cast. Need to debug or give a few var_dump() to find the problem location, looking at this code seems correct.

  • 1

    I found the error... was like this: new DbrControl();


require_once "../model/User.php";


require_once "./UserControl.php"; I did this: require_once "../model/User.php";


require_once "./UserControl.php";


new DbrControl(); , and it worked

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.