News

Archive of all the latest news and blog posts

PHP Problem Solved

RSS Icon

Being a self confessed PHP-geek I’m confident about my knowledge of PHP and whenever something bizarre like this crops up it reminds of the nature of PHP being open source and something born out of community rather than a corporate affair.
This by no means implies that PHP is an underdog to such a system as this really is the first time I have ever encountered a scenario where something doesn’t really work as it should. When I used to a lot of coding using Microsoft’s DirectX platform half finished and wrongly documented functions where just the norm. You learned to live with it.

However, today I rewrote part of my application framework to contain the main application class as a singleton and avoid the necessity of passing about a reference to it like some sort of code level pass-the-handle-to-the-parcel. I wanted to keep it backwards compatible, and allow passing a reference to an object as an optional parameter into a parent class constructor. I did this using something similar to this:

public function __construct(){
        
func_num_args() $this->app = &func_get_arg(0) : $this->app app::getInstance(); 

Now when this broke (springs flying out) I resorted back to the PHP documentation which shyly states that func_get_arg returns a copy of the arguments, so therefore optional reference parameters just don’t work. I can understand this except PHP5 brought about a new way of handling objects where all objects are copied by reference, and only really copied when a newly referenced version is altered. This saves memory and is quite a cute trick but should also allow me to use func_get_arg to retrieve a parameter as a reference.

To cut a long story short it doesn’t. However there is a workaround using debug_backtrace() which returns an associative array of info, one element of which is the current function arguments – which DO allow you to pass an optional reference.

I don’t recommend anyone starts using debug functions in production code so I just removed the optional parameter passing and instead took a long route round to ensure it wasn’t needed.
Perhaps this will change in newer versions of PHP, perhaps not (security issue?) but in the mean time bear this in mind.

MW.

Bookmark and Share
Posted In Blog By Waracle, Apr 27, 2009
No Comments Yet. Be the first...

Related Articles
Whats New Bottom
Most Popular

No News Articles

Whats New Bottom
Latest Articles
Whats New Bottom