• October 28, 2006

    some notes on php design

    Today, I have read on metapundit.net some thoughts on how to improve php coding. Don't see the real point in this, except the faster coding. Readability and maintenance is degraded slightly.

    Update - 18.11.2006. - After taking some time to think about this, I have to agree with MetaPundit. Putting var names into array, and looping trough them makes code more maintainable. But I still have some issues with this, regarding more complex stuff.

    Example 1:
    class foo{
    function foo($duration, $height, $width, $quality){
    $this->_duration = $duration;
    $this->_height = $height;
    $this->_width = $width;
    $this->_quality = $quality;
    }
    /* Useful code goes here*/
    ...
    class foo{
    function foo($duration, $height, $width, $quality){
    foreach(array('duration', 'height', 'width', 'quality') as $arg)
    {
    $this->{"_$arg"} = $$arg;
    }
    }
    Example 2:
    

    $row = $db->query('select * from foo');
    $template->setVariable('fname',$row['fname']);
    $template->setVariable('lname',$row['lname']);
    $template->setVariable('zip',$row['zip']);
    $template->setVariable('city',$row['city']);
    $template->setVariable('state',$row['state']);
    $template->setVariable('street',$row['street']);
    $template->setVariable('county',$row['county']);

    ...
    $row = $db->query('select * from foo');
    foreach(array('fname','lname','zip','city','state','street','zip','county') as $field)
    $template->setVariable($field,$row[$field]);
    ...

Reader comments:

  1.  

    November 18, 2006 @ 07:46

    metapundit says:

    Well if you read the article you’ll notice that I don’t actually
    recommend the “after” method in the first example. In the second
    example, however, the “after” method is not only shorter to type the
    first time, it’s much more maintainable. The “before” method requires
    you to copy and paste a line (say the bottom one) and then change
    “country” to the name of your new field. What are the odds that you
    will type

    $template->setVariable(‘mynewfield’,$row['mynewfeild']);

    See the error? I bet you didn’t immediately. But if you use the
    “after” method you only need to add ‘mynewfield’ to the list of column
    names… There are of course advantages to succintness other than
    maintainability (see Succintness = Power at
    http://www.paulgraham.com/power.html ), but I’m basically assuming
    people agree about that already…

  2.  

    November 18, 2006 @ 16:17

    seven says:

    Hi Mate,

    Thank you a lot for your comment. Cant believe you replied. :) I respect your blog, and read it on day to day basis.

    I have to say, that I agree with you on every point. I am in constant search for ways to make my code more maintainable and at the moment, I am not very satisfied with maintainability of my OOP code in php and actionscript (Flash).

    But this example is very simple, and using foreach serves it’s purposes.

    
    
    $row = $db->query('select * from foo');
    foreach(array('fname','lname','zip','city','state','street','zip','county') as $field)
    $template->setVariable($field,$row[$field]);
    

    But, if you need to do something do data which comes from DB before you set that value to the template, (eg. stripslashes, or htmlencode, or some kind of mathematical stuff) it’s impossible. In that case you will have to use longer syntax. Or you will have to develop template functions and put those primitives inside of template functions. I use smarty, so It’s not much of a problem.

    Best regards
    Neven

  3.  

    November 18, 2006 @ 21:14

    metapundit says:

    Hey I’m just happy somebody’s reading the stuff I put out. Checking my referrers and seeing who’s reading and replying is half the fun of blogging… I added a link back to your blog from the article.
    -regards
    metapundit

  4.  

    November 20, 2006 @ 10:50

    seven says:

    yo metapundit! Thank you very much for the response! Keep up the good work mate, there are many readers of your blog. Our blog has like 5 readers tops! Maybe after redesign we will gain some momentum. :)

  5.  

    April 11, 2008 @ 08:52

    jehutyX says:

    my opinion maybe sounds weird, but why do you all write so serious comments? I mean, do you REALLY think it’s true? Why? What makes you think so? Please share your opinion.

Have your say:

 
This blog is Gravatar friendly. Don't have Gravatar? Get one fast!
 

Proudly running on Word Press, and above all, proudly using Comic Sans.

Nivas.hr © Copyright 2009    All right reserved    Made in Croatia Yeah, we made our own site!Nivas.hr