Help with PHP class concept

Hey guys,

I’m hoping ye can help guide me here as I’ve reached a point in PHP where I think I can say I’m no-longer a beginner and would like to advance my knowledge.

I believe my next step is understanding and using classes in PHP but for the life of me I can’t understand the concept(why not just include other PHP files?). So could someone please explain why we whould use them(in simple terms), a link to a good easy to understand tutorial on classes and also if you could suggest an easy class to make to get me started.

Or if you think there is something other than classes I could be lookin at could you please let me know? Thanks.

Classes are the technique that are used, but the reason and the why’s for using them are all about objects.

Here are the basics of classes

Our PHP Application Design forum has a good sticky thread on [url=“http://www.sitepoint.com/forums/showthread.php?t=78687”]advanced PHP resources

A part of it, called Object Oriented Programming and PHP has many resources relating to classes that you may find to be useful.

The analogue to classes would be more like functions. You still need to include code in either situation.

I have something that you can use to try to understand classes. Try to model PHP code to simulate a car and the actions on that car.

Some actions (and parameters):

  1. drive -> number of miles/km
  2. stop
  3. refuel -> x gallons

All while keeping track of these two properties:

  1. distance traveled
  2. gallons in the tank

Write both using two approaches: (1) only functions and (2) only classes. No globals. You should see how objects are much more suited to this situation, and then you can tackle the developments beyond basic classes later.

Hint: For the approach with functions, you can pass around an array.

Just for the record, anything you can do with an OO model, you can do procedurally. Just knowing OOP doesn’t necessarily mean you’re more advanced than a programmer who utilizes a procedural approach. PHP’s I/O functions and the MySQL C API, for example, are nice demonstrations of procedural design.

In PHP5+, the OO model is greatly improved and often a nice fit when designing your applications. One of the main benefits of OOP is presenting a uniform interface for related tasks. However, you don’t have to keep passing opaque variables (e.g., resource variables). Objects encapsulate behavior and state.

One example I like for OOP is output devices. Printers, files, sockets, etc. operate very similarly on a conceptual level, so it makes sense for such objects to share (some of) the same interface. So, they might each have a write, open, and close method. Also, you can combine the procedural and OOP paradigms, if it makes sense for your situation.

<?php

// Interface for output devices
interface OutputDevice
{
  // ...
  public function write($data);
  public function close();
  // ...
}

// Do something with a generic output device
function foobar(OutputDevice $out)
{
  // ...
  $out->write($baz);
  // ...
}

// The "Socket" class would implement the OutputDevice interface
$sock = new Socket(7005, Socket::AF_INET6);
foobar($sock);
$sock->close();

$printer = new Printer('Laser Quux 9000');
foobar($printer);
$printer->close();