Error calling Setter PHP method with Interface!

Asked

Viewed 39 times

-1

Talk guys, all beauty?

I am studying Interfaces in PHP and am having an error invoking the Setter method in my attributes.

<!-- User Interface -->
<?php
session_start();
interface User {

public function userName($uName);
public function userEmail($uMail);
public function userPasswd($uPass); 

}

class Cliente implements User{
private $userName;
private $userEmail;
private $userPasswd;

/**
 * Get the value of userName
 */ 
public function getUserName()
{
    return $this->userName;
}

/**
 * Set the value of userName
 *
 * @return  self
 */ 
public function setUserName($userName)
{
    $this->userName = $userName;

    return $this;
}

/**
 * Get the value of userEmail
 */ 
public function getUserEmail()
{
    return $this->userEmail;
}

/**
 * Set the value of userEmail
 *
 * @return  self
 */ 
public function setUserEmail($userEmail)
{
    $this->userEmail = $userEmail;

    return $this;
}

/**
 * Get the value of userPasswd
 */ 
public function getUserPasswd()
{
    return $this->userPasswd;
}

/**
 * Set the value of userPasswd
 *
 * @return  self
 */ 
public function setUserPasswd($userPasswd)
{
    $this->userPasswd = $userPasswd;

    return $this;
}

public function userName($uName)
{              
    $this.setUserName($uName);
    echo    "<br>".
            "<p>Nome:".getUserName()."</p>";         

}

public function userEmail($uMail)
{
    $this.setUserEmail($uMail);
    echo    "<br>".
            "<p>Email:".getUserEmail()."</p>";

}

public function userPasswd($uPass)
{

    $this.setUserPasswd($uPass);
    echo    "<br>".
            "<p>Email:".getUserPasswd()."</p>";       

}



}

$usuario = new Cliente();

if(isset($_POST["name"])){
    $usuario->userName($_POST["name"]);
}

if(isset($_POST["email"])){
    $usuario->userEmail($_POST["email"]);
}

if(isset($_POST["passwd"])){
    $usuario->userPasswd($_POST["passwd"]);
}

?>

And the Error:

Fatal error: Uncaught Error: Call to undefined function setUserName() in D:\Programacao\Xampp\htdocs\PHP7\interface\exe02.php:79 Stack trace: #0 
D:\Programacao\Xampp\htdocs\PHP7\interface\exe02.php(109): Cliente->userName('Vitor') #1 {main} thrown in D:\Programacao\Xampp\htdocs\PHP7\interface\exe02.php on line 79

I don’t think the error is related to the Interface, but I don’t understand it returning Undefined in an existing and Public method.

The data is coming from a form on another page, I believe is irrelevant to the problem.

Thanks for your attention

  • 1

    Incorrect syntax in some snippets. nothing else is ever used to invoke methods. "->" vc points and Svezes omits $this->

  • 1

    Yes, the answer below indicated me those syntax errors, but thanks anyway

1 answer

3


Your script is full of errors, first:

$this.setUserEmail($uMail);

The $this in PHP usa -> and not point, should be:

$this->setUserEmail($uMail);

Second, class methods should always be accessed via $this in the case of objects and if they are static with self:: (or parent:: which in this case would be equivalent to super() in Java), this:

"<p>Email:".getUserEmail()."</p>";

This should be it:

"<p>Email:".$this->getUserEmail()."</p>";

At more than one place the script has these 2 problems, fix them all and will probably work.


Another problem (which may be a problem or not, if you are using buffer), session_start uses the HTTP header to declare cookies, every header of an HTTP response (every site is HTTP) sends the headers before the body, but at the time you added

<!-- User Interface -->

Before the session_start you will be forcing PHP to send the body and the session_start will not work because it will be impossible to send the header

Read more on:

  • 1

    Thank you very much! It was my inattention regarding the use of $this, I fixed it and it worked perfectly! As for the header issue, I didn’t even know that this problem could occur, thanks again!!

Browser other questions tagged

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