Add Me!Close Menu Navigation

PHP | Web Development | Object Oriented | MVC | MySQL | Apache

Add Me!Open Categories Menu

References and object access in PHP

Reading some code this morning I realised that some people use overcomplicated object access syntax. In OOP, some objects can be deep in the structure. Calling their methods or accessing their attributes involves long lines and hard to read code as shown bellow.

<?php

$fu1 = $someobject->otherone->yetanother->lastone->bar( 1 );

$fu2 = $someobject->otherone->yetanother->lastone->bar( 2 );

?>

Just say no! This is the perfect moment to use references!

<?php

$myref = &amp;$someobject->otherone->yetanother->lastone;

$fu1 = $myref->bar( 1 );

$fu2 = $myref->bar( 2 );

?>

References save a lot of hassle in many circumstances. In this example, where we have an object accessed multiple times using the same long path using a reference offers interesting benefits. The code is easy to read and maintenance is simplified.

More soon.

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

Posted By admin

4 Responses to “References and object access in PHP”

  1. Fab says:

    I hope it wasn’t my code XD
    Good luck for your blog

  2. mbp001 says:

    It is still to far reaching.

    If lastone is supposeed to be public then someobject should have a direct public accesor eg. getLastone. Otherwise changing otherone or yetanother could breaks things very heavily throughout the whole system.

    Law of demeter
    http://en.wikipedia.org/wiki/Law_of_Demeter#OOP
    http://www.ccs.neu.edu/home/lieber/LoD.html
    http://www.ccs.neu.edu/research/demeter/demeter-method/LawOfDemeter/paper-boy/

  3. admin says:

    Very true! Sadly the existing code on many systems does not comply with the LoD. I will post a proper solution to the problem above soon.

  4. mbp001 says:

    In response to
    “interesting point you have made on my blog about the LoD-F. The issue here is that some frameworks rely on coding such as the example I made. Is it good practice? No. I’m just trying to bring simplification to a coding problem that exists in many different systems. I had to read some pretty mind wrecking Zend framework code lately and that was my primary response.

    Fair enough, the point in your example does help in breaking deep dependancy at the earliest possibility. All code from that point on is oblivious to the deep rooted connection it was originally held in.

    “It might have been the wrong response but I sadly couldn’t just ditch 2 years of development predating my presence.”

    It is very easy to be elegant when doing something afresh. Making something elegant that isn’t is far harder. Good development is a slow fight, anyone can scream rewrite/throw it away move to another framework etc. Or more likely the case just put no mental effort into it and sulk if they have to work with it. I didn’t realise this was a reaction to something outside of your control, I have seen it a lot from people where it is within their control.

    Bad software is like a puzzle that needs to be solved, the rules have been laid out but are hidden. It seems like you have a knack/ interest for solving those problems, it is not for someone in for the short haul but will always give back if the fight is put in ;) . The fight never ends though which is what makes it so captivating :)

    Sometimes that fight is as much with other developers as the code itself. Though to be pedantic it is always with other developers as they wrote the code in the first place ( externalising all developers to be a singular external force a bit there but I find it helps to see a pond with people on the outside throwing stones in and I have to keep it calm ).

    “I will probably add another post with wrappers and proper syntax soon.”

    Adaptors( what you might be referring to as wrappers ) are pretty handy. It is about managing debt( http://martinfowler.com/bliki/TechnicalDebt.html ) and understanding what debt is. Working with any framework outside of your control has some debt as nothing is every free( note: actually all code has debt, it is about degrees ), though I am not saying don’t ever work with any framework just keep your core logic at arms length from it. Don’t put anything in it’s prescribed coding structure except the bare minimum to get away. If you find stuff there from other developers refactor it elsewhere.

    That is not condescending to you as it might seem obvious, but this is a public discussion so imaginary wanderers always have to be addressed.

    Anyway fun discussion…. Much more fun than “My SQL query doesn’t work and in my example I am going to show I am copying a pasting my error handling at least 100 times verbatim and burn your eyes in the processs” anway :)

Leave a Reply




Tag Cloud

Categories

Archive

Pages

Popular Posts