TechTip: PHP Classes: Will This Ever Be Over?

Scripting
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

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 termin this case, publicand 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.

David Shirey

David Shirey is president of Shirey Consulting Services, providing technical and business consulting services for the IBM i world. Among the services provided are IBM i technical support, including application design and programming services, ERP installation and support, and EDI setup and maintenance. With experience in a wide range of industries (food and beverage to electronics to hard manufacturing to drugs--the legal kind--to medical devices to fulfillment houses) and a wide range of business sizes served (from very large, like Fresh Express, to much smaller, like Labconco), SCS has the knowledge and experience to assist with your technical or business issues. You may contact Dave by email at This email address is being protected from spambots. You need JavaScript enabled to view it. or by phone at (616) 304-2466.


MC Press books written by David Shirey available now on the MC Press Bookstore.

21st Century RPG: /Free, ILE, and MVC 21st Century RPG: /Free, ILE, and MVC
Boost your productivity, modernize your applications, and upgrade your skills with these powerful coding methods.
List Price $69.95

Now On Sale

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  •  

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: