some notes on php design

Author: seven October 28, 2006

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]);
...
Author
seven
CEO/CTO at Nivas®
Neven Jacmenović has been passionately involved with computers since late 80s, the age of Atari and Commodore Amiga. As one of internet industry pioneers in Croatia, since 90s, he has been involved in making of many award winning, innovative and successful online projects. He is an experienced full stack web developer, analyst and system engineer. In his spare time, Neven is transforming retro-futuristic passion into various golang, Adobe Flash and JavaScript/WebGL projects.

    5 thoughts on “some notes on php design”

  • 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…

  • 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

  • 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

  • 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. :)

  • 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.

  • Leave a Reply

    Your email address will not be published. Required fields are marked *