About

Andrew Culver, a web development manager, has been very busy this year and hasn’t made the time to update his blog. Shame on me.

Statements and Evaluations

March 11th, 2008

Introduction

How many evaluations take place on each of the following three statements?

  1. class{
  2.  public function() {
  3.   // Statement one.
  4.   $this->_name = $name;
  5.  
  6.   // Statement two.
  7.   $accounts = $this->_accounts->get($return);
  8.  
  9.   // Statement three.
  10.   $this->_replies = new Relationship_OneToMany(‘Email’,
  11.    Finder_Factory::create(‘Email’)->findByParent($this),
  12.    $this->_replies);
  13.  }
  14. }

If your answers for each statement were at least 3, 5, and 10, you are hereby dismissed for the remainder of the post. If you (like most I’ve asked) answered 1 for the first statement, please stay awhile. I suspect this post may aid you in becoming a stronger programmer.When we’re done, you should hopefully have a clear understanding of the terms:

  • Statement
  • Evaluation

Statements and Evaluations

You might think of a statement as a line of code with a semicolon at the end or the conditions we put in a while loop. While that’s true, a more generic and granular definition would be to think of a statement as anything you can wrap parenthesis around and smack an echo in front of. When you think of statements this way you realize that each line of code is often made up of many, many statements, all of which need to be evaluated individually in the process of evaluating the entire larger statement.So our first statement had one assignment. That resulted in the following evaluations:

  • The entire right hand side, which we’ll say was $name evaluating to a string.
  • The $this variable needed to evaluate to an instance of an object to set a property on.
  • Assignments don’t only assign the value, but also evaluate to whatever value was assigned.

(Depending on your knowledge of the PHP engine you may be able to come up with a better number but I think those are sufficient for practical purposes.)

Order of Evaluation

Knowing the order in which these smaller statements are evaluated can also be helpful.A few rules might help:

  • Statements are evaluated as they’re needed.
  • Function (or method) parameters are evaluated before a function is called.
  • Think of the return statement as the function saying “I’m done! I evaluate to …”

For example, consider our second statement:

  • If we’re going to evaluate an assignment, we must first evaluate the right hand side.
  • If we’re going to evaluate the get() method, the $this->_accounts property must first evaluate to an object.
  • If we’re going to evaluate the $this->_accounts property, $this itself must first evaluate to an object.
  • Now we’ve got the object from the property but before we can evaluate the get() method, we must evaluate it’s parameter.
  • With that we can call the get() method which evaluates to an array. (Its return value.)
  • We can finally perform the assignment which itself evaluates to whatever value was assigned.

Why Know It

Consider our third statement. Not only does it have many (10+) smaller evaluations going on, but the object-oriented syntax and execution can be a little daunting to the uninitiated. But being armed with a solid understanding of the concepts we’ve covered will enable you to more easily jump into whatever code is thrown at you. Rather than struggle with what seems like complicated syntax, you’ll be able to break it down, understand it, and debug it with greater ease. It’s a precursor for recognizing the use of design patterns in another developers code. Do whatever it takes to understand how and in what order complex statements evaluate!

No Comments »

Basic Design Patterns

March 9th, 2008

Zend PHP 5 Certification Study Guide (Smaller)

This sounds terrible, but I lent a copy of the “Zend PHP 5 Certification Study Guide” to my 15-year-old cousin Ben. What makes it terrible is that I myself had it on loan from my boss. I was searching among the titles available to me for a book that served as a good primer for an entry-level programmer with little-to-no exposure to PHP. After browsing the table of contents, I found myself impressed with the range of topics covered, especially taking into account the actual size of the book, which gives me hope that Ben will actually be able to pay attention for long enough to get the important details. Only time will tell. :)

What caught my attention was a section titled “Design Pattern Theory”. It contains 5 pages giving a very, very basic introduction to 5 common object-oriented design patterns, and sample implementations and usage for 3 of them. My take away is this: As PHP programmers we should (at the very least) be able to recognize the following patterns in usage:

  • Singleton
  • Factory
  • Registry
  • Model-View-Controller
  • ActiveRecord

I was pleased to see that a basic understanding of these common patterns is a requirement to obtain the title of “Zend Certified Engineer”. These are just the tip of the iceberg. In addition to these, I find myself fairly frequently giving explanation of how to read and understand code that makes use of a lot of object-orientation or less obvious design patterns. I’m going to make the effort to start documenting those topics here, at least for the benefit of those developers I know.

No Comments »

Syntax Highlighting

March 6th, 2008

Please forgive me as I attempt to get syntax highlighting working to my liking.

  1.  
  2. <?php
  3. /**
  4.  * Some class
  5.  * @author Andrew Culver <andrew.culver@gmail.com>
  6.  */
  7. class Some_Class
  8. {
  9.     protected function something()
  10.     {
  11.         return "Some result!";
  12.     }
  13. }
  14.  
  15. // Some comment.
  16. $output = Some_Factory::create(‘Some_Class’)
  17.     ->setSomeProperty(‘Some Value’)
  18.     ->something();
  19. echo "The result was ‘{$output}’.\n";

Wow. After a little tweaking, I’m really happy about that. The results are pretty much the same as my TextWrangler settings.

No Comments »

I wonder if having a disclaiming “about” line like that will help me loosen up and publish something. :) Well, the title is “principles, patterns, and practices.” Perhaps we can talk about just that!

No Comments »