Skip to content
Advertisement

Instantiate object from an accessor

I have a strange issue with php and instantiating objects:

Background

There’s a class structure I’ve created (some interfaces, some traits etc) where I automatically extend a class with a whole load of functions etc. These are working as expected in almost all scenarios.

I’ve attempted to close things down to “idiot/public” proof this repo, so I have made sensitive properties/methods private/protected.

This set of functions include object generation and it is done something like as follows:

JavaScript

(obviously this is done over a few files, so I’m simplifying as it completely illustrates the issue)

So, in their various classes and for various inheritance/anti-pebkac reasons I want to do the following:

JavaScript

This last line generates an error:

ErrorException: Undefined property: (...)SomeController::$getObjectName

If you do the following:

JavaScript

.. it outputs:

JavaScript

Both are exactly the same.

Question

How do you instantiate an object from a string accessor?

Advertisement

Answer

After some messing around, I discovered that this needs to be done on separate lines:

JavaScript

I will raise this as a bug in php.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement