We're going to keep chipping away at what PHP classes are and how they can be used. So here we go for installment 3 of I don't really know how many.
If you tuned in to last month's PHP TechTip, you know that we talked in greater detail about how to set up a PHP class. Specifically, you found out that…
- A class consists of both local properties (variables) and methods (or "functions" as we generally refer to them).
- The definition of the local variables (properties) included a term to indicate the security level (public, private, protected) assigned to each variable.
- There was no picture information for the local variables. PHP is a loosely typed language, and the type of variable we are creating is actually set when we assign a value to the variable ($name = 'Dave' defines a variable-length string element).
- The methods or functions defined (and you can have more than one in a class) represent the actions you want PHP to take whenever a new object is created in this class. The process of creating a new object is called "instantiation" as in we will "instantiate" a new object of this class.
- The method can also be assigned a security level (public, private, protected) just like the data.
So far, so good. After that, things broke down just a bit. As it turns out, the code I gave you for instantiating the object didn't work as well as I had hoped. Or at all. I screwed up, and I'm sorry about that. Fortunately, Chris Ringer was careful to point out my errors (every last, single one of them), so I'm going to do this again, and this time I'll actually test the code (which I typically do anyway). I will say, in my defense, I was trying to really simplify things, but I still managed to screw it up.
What it should've looked like is this. (The line numbers are for reference, they are not part of the PHP script.)
1 Class name
2 { public $firstName;
3 public $lastName;
4
5 public function __construct($firstName, $lastName)
6 { echo 'My name is' . ' ' . $firstName . ' ' .
7 $lastName;
8 }
9 }
Now, let's take this apart and see what we have.
Line 1:
"Class" is the operator that indicates I'm defining a class, a blueprint for the objects we'll create with this class.
"name" is the name of this class. I admit that in this case it seems a bit confusing (its name is name), but I'm stubbornly hanging on to this. I'm not sure why.
There are no parms associated with the class definition, although sometimes parms may be associated with creating an object from this class. In a way, all of the data items defined in the class are its parms.
Lines 2 and 3:
Next comes the specification of the local data elements that are used in this class.
Note there's the security term—in this case, public—and the name but no type or size information.
Line 5:
Next comes what I forgot in last month's example, the "constructor." The constructor is a special PHP system method that creates objects from the class blueprint. It starts with the security tag and is followed by the canned verbiage function __construct. There are two underscores in front of the construct. It also includes two parms that will be passed in whenever this constructor is called.
Lines 6 and 7:
These lines show what happens when we run the constructor. They're part of the method. Real PHP people won't like what I've done. Normally, you would just initialize the class fields and then do stuff in other methods, but I'm trying to keep this very simple, so I'm doing an echo function (to prove that the constructor has run) as part of the constructor rather than as a separate function. I'll offer a more correct and more OO and more complex-looking way to do this next month when we finally finish creating our true OO class. But for now, I'm opting for simplicity and transparency.
"echo" will print something to a web page. Constants are enclosed in quotes (single or double). The period is the concatenation symbol. The spaces in quotes are required to separate the first name and last name values. —And finally, we have the variable names $firstName and $lastName.
Lines 8 and 9:
At the end, you have the closing brace for the constructor function (line 8) and the closing brace for the class definition (line 9).
And this is a class definition. One that works. Remember, it's the blueprint for an object, but the question remains: how do you create an object from this definition?
Generally, the class definition is kept in a separate file ("source member" in our language) that can then be pulled into code as it's needed. For the moment, we're going to put the code to create the object directly into this script. Just remember that generally won't be the case.
To create a member of this class, simply execute this code:
$firstName = 'David';
$lastName = 'Shirey';
$myName = new name($firstName, $lastName);
What's happening here is very simple.
First, on lines 1 and 2, we're assigning values to the first name and last name variables.
Then, on line 3, we're taking a new variable that hasn't been defined in the class and using it to "call" the class definition. (In PHP, you can define variables on the fly, and that's what I'm doing here.) This is similar to the function call in RPG ILE. The word "new" tells the system what this variable is going to hold: it's going to hold a new object or member of a class. And the "name" tells PHP exactly what class you're instantiating a new object in. That's followed by the parms that represent the data elements defined in that class.
The result of issuing these commands will be to display the following words on the web page:
My name is David Shirey
Remember, you will run this from the URL line of a browser, and the words will appear on the blank page of that browser.
What's Next?
The above code is still a simplification of the type of code you will normally see. But again, my time is over. So look next month for a slightly more rigorous formulation of this. For the moment, though, get familiar with how this works. We'll add more detail later.
LATEST COMMENTS
MC Press Online