To design your program or code library in an object-oriented
fashion, you'll need to define your own classes, using the class
keyword.
A class definition includes the class name and the properties and methods of the class. Class names are case-insensitive and must conform to the rules for PHP identifiers. The class name stdClass is reserved. Here's the syntax for a class definition:
A class definition includes the class name and the properties and methods of the class. Class names are case-insensitive and must conform to the rules for PHP identifiers. The class name stdClass is reserved. Here's the syntax for a class definition:
class classname [ extends baseclass ] { [ var $property [ = value ]; ... ] [ function functionname (args) { // code } ... ] }
Declaring Methods
A method is a function defined inside a class. Although PHP
imposes no special restrictions, most methods act only on data within the object
in which the method resides. Method names beginning with two underscores (_
_) may be used in the future by PHP (and are currently used for the object
serialization methods _ _sleep( ) and _ _wakeup( ), described
later in this chapter, among others), so it's recommended that you do not begin
your method names with this sequence.
Within a method, the $this variable contains a
reference to the object on which the method was called. For instance, if you
call $rasmus->birthday( ) inside the birthday( ) method,
$this holds the same value as $rasmus. Methods use the
$this variable to access the properties of the current object and to
call other methods on that object.
Here's a simple class definition of the Person class
that shows the $this variable in action:
class Person {
var $name;
function get_name ( ) {
return $this->name;
}
function set_name ($new_name) {
$this->name = $new_name;
}
}
As you can see, the get_name( ) and set_name(
) methods use $this to access and set the $name property
of the current object.
To declare a method as a static method, use the static keyword.
Inside of static methods the variable $this is not defined. For
example:
class HTML_Stuff {
static function start_table( ) {
echo "<table border='1'>\n";
} static function end_table ( ) {
echo "</table>\n";
}
}
HTML_Stuff::start_table( ); // print HTML table rows and columns HTML_Stuff::end_table( );
Declaring Properties
In the previous definition of the Person class, we
explicitly declared the $name property. Property declarations are
optional and are simply a courtesy to whoever maintains your program. It's good
PHP style to declare your properties, but you can add new properties at any
time.
Here's a version of the Person class that has an
undeclared $name property:
class Person { function get_name ( ) { return $this->name; } function set_name ($new_name) { $this->name = $new_name; } }
You can assign default values to properties, but those default
values must be simple constants:
var $name = 'J Doe'; // works var $age = 0; // works var $day = 60*60*24; // doesn't work
Using access modifiers, you can change the visibility of
properties. Properties that are accessible outside the object's scope should be
declared public; properties on an instance that can only be accessed by
methods within the same class should be declared private.