0

im creating a simple php user system, i have a user class and i`m storing the new user values using this function (the Add) inside user class:

    private function NewUser($nome, $email, $tel, $cidade, $estado, $aniversario) {
    $this -> name = mysql_real_escape_string($nome);
    $this -> email = mysql_real_escape_string($email);
    $this -> telefone = mysql_real_escape_string($tel);
    $this -> cidade = mysql_real_escape_string($cidade);
    $this -> estado = mysql_real_escape_string($estado);
    $this -> dia = substr($aniversario, 0, 2);
    $this -> mes = substr($aniversario, 3, 2);
    $this -> ano = substr($aniversario, 6);

    }

    public function Add($nome, $email, $tel, $cidade, $estado, $aniversario) {
    $this -> NewUser($nome, $email, $tel, $cidade, $estado, $aniversario);
    $Create_query = "INSERT INTO user (nome, email, telefone, cidade,    estado,      dia, mes, ano) VALUES ('{$this -> name}', '{$this -> email}',
    '{$this -> telefone}', '{$this -> cidade}', '{$this -> estado}', '{$this -     > dia}', '{$this -> mes}', '{$this -> ano}')";
    $query_user = connectdb::sql_query($Create_query); // my connect class
    if ($query_user) {
        $answer = array('done' => 'done');
        echo json_encode($answer);
    }
    else {
        validate::returnError();
    }
     }

If i try to run the insert with $this -> attribute or '$this -> attribute' i got a Object of class User could not be converted to string error,but if i run then as '{$this -> attribute}'it works. I have found these {} method, in a stack overflow answer, but theres no detail`s, can someone explain why this work, and if its safe/correctly? Thank you for your time.

1
  • Do you still get the error when you remove the spaces before and after ` -> ` everywhere in your code? E.g. $this -> name would become $this->name, and likewise everywhere else in your code. Commented Oct 26, 2013 at 23:24

1 Answer 1

1

To answer your question about the curly braces: Curly braces are used to explicitly specify the end of a variable name.

Some examples can be seen here: http://docs.php.net/manual/en/language.variables.variable.php

As for the error (Object of class User...): The problem there are the whitespaces in $this -> attribute. Since you are using them inside of a string the whitespaces are getting interpreted as what they are, whitespaces. So it tries to convert just $this to a string, which is not possible (see the error). If you use the the curly braces php knows exactly when the variable starts and when it ends.

On another note: try to use PDO instead of simple mysql_query...Have a look here: http://php.net/manual/de/book.pdo.php

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.