PHP Tutorial – 10 – Class
A class is a template used to create objects. It is defined with the keyword class followed by a name and a body, which contains the definitions of fields and methods. Fields are variables that hold the state of the object, while methods define what the object can do. A field in PHP must have an explicit access level specified. For now the public access level will be used which gives unrestricted access to the field.
class MyRectangle { public $x, $y; function calcArea($a, $b) { return $a * $b; } }
The var keyword may also be seen when fields are declared in PHP 4 code. This keyword gives public access just as the public modifier. However, as of PHP 5 this keyword has become deprecated and should not be used.
var $x, $y; // deprecated field declaration
Constructor
The class can have a constructor. This is a special method used to initialize the object which starts with two underscores followed by the word construct.
class MyRectangle { public $x, $y; function calcArea($a, $b) { return $a * $b; } function __construct() { echo "Constructed"; } }
Instantiating an object
When creating a new instance of this class, using the new keyword, the constructor will be called.
$r = new MyRectangle(); // Constructed
Accessing object members
In order to access members that belong to this object the single arrow operator (->) is used. This could for example be used to call the method or to assign some values to the fields.
$r->calcArea(10, 2); // 20 $r->x = 5; $r->y = 10;
A better way to initialize the fields would be through the constructor. To access members from inside of the class the “this” pseudo variable must be used along with the single arrow operator (->). The “this” variable is a reference to the current instance of the class and can only be used within an object context. Without it x and y would just be seen as local variables.
class MyRectangle { public $x, $y; function calcArea($a, $b) { return $a * $b; } function __construct($a, $b) { $this->x = $a; $this->y = $b; } } $r = new MyRectangle(5,10);
Initial field values
If a field needs to have a default constant value one way to assign it would be in the constructor. Another, cleaner way is to assign the field at the same time as it’s declared. This initial value is set implicitly when the object is created. Assignments of this kind must be a constant expression and cannot be a variable or a function call.
class MyRectangle { public $x, $y, $z = 15; }
Destructor
In addition to the constructor classes can also have a destructor. This method starts with two underscores followed by the word destruct. It will be called as soon as all references to the object are removed before the object is destroyed.
class MyRectangle { // … function __destruct() { echo "Destructed"; } }
To test the destructor the unset method can be used to manually remove the references to the object.
unset($r); // Destructed
Note that the object model has been completely rewritten in PHP 5. Therefore, many features of classes, such as destructors, won’t work in earlier versions of the language.
Case sensitivity
Class names in PHP are case insensitive – just as function names, keywords, and built-in constructs such as echo. This means that a class named myclass can also be referenced as MyClass or MYCLASS.
class myclass {} $o1 = new MyClass(); // ok $o2 = new MYCLASS(); // ok
If you like this tutorial please +1 it:


![[Affiliate link] Total Training]( http://d3qzmfcxsyv953.cloudfront.net/images/pvt-affiliates/totaltraining.png)
![[Affiliate link] Lynda](http://d3qzmfcxsyv953.cloudfront.net/images/pvt-affiliates/lynda.png)


how you learn php,
Thanks John. You pretty much summed up the goal of our tutorials. Each tutorial section is designed to be short and to the point, explaining only what is both relevant and practical to each section, without any repetition or needless theory.
The shortest yet complete explanation I have seen. Great job, keep up the excellent works.