PHP Problem Solved

0

Being a self confessed PHP-geek Im 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 doesnt really work as it should. When I used to a lot of coding using Microsofts 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:
<span style="color: #000000">
<span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">__construct</span><span style="color: #007700">(){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">func_num_args<span style="color: #007700">()</span>&nbsp;</span><span style="color: #007700">&gt;&nbsp;</span><span style="color: #0000BB">0&nbsp;</span><span style="color: #007700">?&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">app&nbsp;</span><span style="color: #007700">=&nbsp;&amp;</span><span style="color: #0000BB">func_get_arg</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">)&nbsp;:&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">app&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">app</span><span style="color: #007700">::</span><span style="color: #0000BB">getInstance</span><span style="color: #007700">();&nbsp;</span>
</span>
Now when this broke (springs flying out) I resorted back to the PHP documentation which shyly states that func_get_arg returns a <i>copy</i> of the arguments, so therefore optional reference parameters just dont 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 doesnt. 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 dont 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 wasnt needed.
Perhaps this will change in newer versions of PHP, perhaps not (security issue?) but in the mean time bear this in mind.

MW.

Latest News

Sign up for our mailing list.

Our Apps

Scotland National Teams

SFA - version 1.3

more £0.69

Voucher Spree

Spree Media Group - version 2.1

more FREE

Love Food Hate Waste

Zero Waste Scotland - version 1.5

more FREE
All

Latest Tweets