Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call by reference of properties don't call advices ! #71

Open
baptistepillot opened this issue Aug 28, 2013 · 0 comments
Open

Call by reference of properties don't call advices ! #71

baptistepillot opened this issue Aug 28, 2013 · 0 comments

Comments

@baptistepillot
Copy link

Look at this code, which is some kind of "default value initializer" for a property.

This is used in my development of a framework including some getters that read linked data on the fly : the property value is read from the DAO only when someone want to read it.

The example above is a simplified version of what I do, with a static initialization.

class Classy
{
    public $prop;
}

function propInit(AopJoinpoint $joinpoint)
{
    $object = $joinpoint->getObject();
    $property = $joinpoint->getPropertyName();
    if (!isset($object->$property)) {
        $object->$property = array("my", "default", "values");
    }
}

$object = new Classy();
echo print_r($object->prop, true);

// Will display :
// array(0 => my, 1 => default, 2 => values)

But now I replace my echo by something more complicated. In some cases I need call-by-reference. Remove the echo and replace it with it :

function propDisplay(&$prop)
{
    echo print_r($prop, true);
}

$object = new Classy();
propDisplay($object->prop);

// Should display the same :
// array(0 => my, 1 => default, 2 => values)
// Will display :
// nothing !

In this case the advice is not called on propDisplay($object->prop) call, because the value of the property is not read by PHP, but it's reference is sent to the called function.

When inside the function, I do not access to a property but to a local variable, and the hook does not work. propInit() will never be called in this case.

My workaround :

$object->prop;
propDisplay($object->prop);

But I have to think about it each time I call a function that awaits references... not very transparent.

What answer should you provide ?

If possible, please make AOP joinpoints work when I call propDisplay($object->prop), even if the parameters are passed by reference. Could you do that ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant