Statements and Evaluations
March 11th, 2008Introduction
How many evaluations take place on each of the following three statements?
-
class … {
-
public function …() {
-
// Statement one.
-
$this->_name = $name;
-
-
// Statement two.
-
$accounts = $this->_accounts->get($return);
-
-
// Statement three.
-
$this->_replies = new Relationship_OneToMany(‘Email’,
-
Finder_Factory::create(‘Email’)->findByParent($this),
-
$this->_replies);
-
}
-
}
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
$nameevaluating to a string. - The
$thisvariable 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
returnstatement 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->_accountsproperty must first evaluate to an object. - If we’re going to evaluate the
$this->_accountsproperty,$thisitself 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!
