Objects and PHP

The focus of object orientation is the logical grouping of related variables into a generic variable - an object. Before you can create an object, however, you must create a class.

Python vs. PHP: Choosing your next project's language

A class is a template for an object. The following class, Vehicle, is designed to describe attributes of 'any' vehicle.

01	<?
02	class Vehicle
03	{
04 var $doors;
05 var $colour;
06 var $name;
07 var $price;
08
09 function Vehicle($doors,$colour,$name,$price)
10 {
11 $this->doors = $doors;
12 $this->colour = $colour;
13 $this->name = $name;
14 $this->price = $price;
15 }
16
17 function expensive()
18 {
19 if($this->price > 40000)
20 return true;
21 else
22 false;
23 }
24
25 function print_vehstr()
26 {
27 echo "{$this->name}: {$this->colour} {$this->doors} door for \${$this->price}\n";
28 }
29 }
30		 ?>

Notice how the class Vehicle looks like a regular function. Unlike a function, however, it has functions declared inside it.

The second line declares the class, which is called Vehicle. Lines four through seven declare 'properties' of the Vehicle class. Those properties - $doors, $colour, $name and $price - refer to four generic attributes of any vehicle with which our application deals. The remainder of the class definition consists of the declaration of three 'methods': Vehicles(), expensive() and print_vehstr().

The Vehicle() method is called a 'constructor'. We know it is a constructor because it has the same name as the class, Vehicle. Constructors are executed when an object is created out of the class. It sets up the object with any data necessary for use by the script - hence the name, constructor. This constructor sets the class properties declared on lines four through seven. It references these properties via the object $this, which is a reference to the object itself.

The next method, declared on line 17, tells the user whether the vehicle is expensive or not. To do so, it checks whether the $price property is greater than 40000. If so, it returns true - telling the user that the vehicle is expensive; otherwise, it returns false. Again, the method references the class property via the $this object.

The final method, print_vehstr(), prints out a vehicle string containing all properties. The following lines create an object from the Vehicle class.

01 <HTML>
02 <BODY>
03 <?
04 $car = new Vehicle(2,"red","Sport's car",60000.00);
05 if($car->expensive()) {
06 echo "An expensive vehicle\n";
07 } else {
08 echo "Not an expensive vehicle\n";
09 }
10 $car->print_vehstr();
11 ?>
12 </BODY>
13 </HTML>

The fourth line creates an object, $car, from the Vehicle class. The script passes four variables corresponding to the variables in the constructor. (When there is no class constructor, no variables need be passed.)

Line five tests whether the vehicle is expensive and, if so, tells the user. Otherwise, the user is told that the vehicle is not expensive. Finally, on line 10, we print the vehicle string.

You may be wondering why we've gone to all this trouble for such a basic application when the following script would have sufficed.

01 <HTML>
02 <BODY>
03 <?
04 $doors = 2;
05 $colour = "red";
06 $name = "Sport's Car";
07 $price = 60000.00;
08
09 if($price > 40000) {
10 echo "An expensive vehicle\n";
11 } else {
12 echo "Not an expensive vehicle\n";
13 }
14 echo "$name: $colour $doors door for \$$price\n";
15 ?>
16 </BODY>
17 </HTML>

Consider, a situation where there are 10 different vehicles. How would you group the relevant variables? A good project to learn more about objects would be to implement two applications: one using objects, the other standard PHP, but with each storing data on 10 different vehicles. You will quickly see how useful objects can be.

Join the PC World newsletter!

Error: Please check your email address.

Our Back to Business guide highlights the best products for you to boost your productivity at home, on the road, at the office, or in the classroom.

Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Gavin Sherry

PC World
Show Comments

Most Popular Reviews

Latest News Articles

Resources

PCW Evaluation Team

Azadeh Williams

HP OfficeJet Pro 8730

A smarter way to print for busy small business owners, combining speedy printing with scanning and copying, making it easier to produce high quality documents and images at a touch of a button.

Andrew Grant

HP OfficeJet Pro 8730

I've had a multifunction printer in the office going on 10 years now. It was a neat bit of kit back in the day -- print, copy, scan, fax -- when printing over WiFi felt a bit like magic. It’s seen better days though and an upgrade’s well overdue. This HP OfficeJet Pro 8730 looks like it ticks all the same boxes: print, copy, scan, and fax. (Really? Does anyone fax anything any more? I guess it's good to know the facility’s there, just in case.) Printing over WiFi is more-or- less standard these days.

Ed Dawson

HP OfficeJet Pro 8730

As a freelance writer who is always on the go, I like my technology to be both efficient and effective so I can do my job well. The HP OfficeJet Pro 8730 Inkjet Printer ticks all the boxes in terms of form factor, performance and user interface.

Michael Hargreaves

Windows 10 for Business / Dell XPS 13

I’d happily recommend this touchscreen laptop and Windows 10 as a great way to get serious work done at a desk or on the road.

Aysha Strobbe

Windows 10 / HP Spectre x360

Ultimately, I think the Windows 10 environment is excellent for me as it caters for so many different uses. The inclusion of the Xbox app is also great for when you need some downtime too!

Mark Escubio

Windows 10 / Lenovo Yoga 910

For me, the Xbox Play Anywhere is a great new feature as it allows you to play your current Xbox games with higher resolutions and better graphics without forking out extra cash for another copy. Although available titles are still scarce, but I’m sure it will grow in time.

Featured Content

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?