Avoid Overloading of attributes when using magic methods

Asked

Viewed 76 times

1

I have the following PHP class:

<?php
class User{
    private $email = null;
    private $pass = null;

    public function __set($atrib, $value){
        $this->$atrib = $value;
    }

     public function __get($atrib){
        return $this->$atrib;
    }
}
?>

when I was testing I got the following error:INDIRECT MODIFICATION OF OVERLOADED PROPERTY HAS NO EFFECT researched and saw that this error is directly linked to the magic methods so I searched a solution to the problem, I removed the attributes $email and $pass from the class the problem was solved but this lenvanted a question: How I could determine the attributes of the class without generating error: Inderect modification ...?

Example of use:

$user = new User();
$user->email = $_GET['email'];
$user->pass = $_GET['pass'];
loginSearch($conn,$user);
function loginSearch($conn,$user){
        $query = "SELECT email, pass FROM usuarios WHERE email = :email AND pass = :pass";
        $stmt = $conn->prepare($query);
        $stmt->bindParam(':email',$user->email,PDO::PARAM_STR);
        $stmt->bindParam(':pass',$user->pass,PDO::PARAM_STR);
        return $stmt;
    }

Error happens on lines: $stmt->bindParam(':email',$user->email,PDO::PARAM_STR); and $stmt->bindParam(':pass',$user->pass,PDO::PARAM_STR);

Note: Omit connection creation and the execute of stmt.

Screenshot of the error generated: inserir a descrição da imagem aqui

  • You are sure this is the code you are using. I saw an error in it and it is not related to what you were saying. What you’re saying happens I couldn’t reproduce. I did different tests and it always works http://ideone.com/YzRCJz. One thing you didn’t answer, what are you using the magic methods for? They seem to be out of function. In fact they are rarely useful if you want to keep the code organized.

  • Which version of php you’re using?

  • @bigown, here’s a code that mimics the error: https://phpolyk.wordpress.com/2012/07/25/indirect-modification-of-overloaded-property/

  • @bigown I’m using to be able to set and recover values without having to access methods ex: $name = $user->name;

  • But this is not what you are doing, it is no use getting errors that other codes are generating. You have to do on top of yours. If you are doing different than what you are posting it gets complicated. I moved more in the example and keeps running smoothly.

  • @bigown, this error is happening to my code, this posted in question is being printed a notice on the screen with the error, I will add a print

  • Yeah, but there’s some people trying to reproduce and no one can make it happen, so there’s something else that’s missing. If you don’t get consistent information I think no one will be able to help you.

  • @bigown my intention of using the methods __set and __get is not to need the use of function calls to set and recover values of variables, would use the variable as if it were public, the way I am using works however is displayed a notice with the error.

  • @bigown taking into account the notice I believe __set is creating another variable with the same of the already created within the class ($email,$pass)

  • Notice is not error, it is warning that you are doing something not recommended. Even so it does not happen to me you can see. I used, as far as possible, the same code as you. Anyway if you still want me to see it happen: http://answall.com/help/mcve

  • @bigown the codes are in different files, so keep passing the objects of a file to gold

  • It seems to be a PHP bug. See these links: http://stackoverflow.com/questions/9452519/php-notice-when-binding-overloaded-property-in-pdostatementbindparam

  • on google I found these other: http://www.1x1.jp/blog/2006/11/php__get_array.html

  • It seems on the link posted by @Danielomine there is the answer to your problem, according to the explanation PDO::bindParam must receive a reference to a variable, but you are passing the return of the function User::__get which cannot be converted into reference, hence the error.

  • @Cahe is generated a notice but the statment is executed

  • Yes, with 'error' I meant 'notice', anyway the link posted by Daniel contains the explanation for your problem.

  • Thanks for the help!

Show 12 more comments

1 answer

1


When I had this problem, the solution was to declare __get with return by reference.

This can be done through the character &.

Behold:

<?php
class User{
    private $email = null;
    private $pass = null;

    public function __set($atrib, $value){
        $this->$atrib = $value;
    }

     public function &__get($atrib){
        return $this->$atrib;
    }
}
?>

Browser other questions tagged

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