When you declare a class, you need to list all variables and all functions that the object should have - called properties and methods. The composition of a class is shown in 3.1.1. Note that you can only declare variables or functions in braces ({}). 3.1.2 shows how to define three properties and two methods in a class.
3.1.1
class Name extends Another Class
{
Access Variable Declaration
Access Function Declaration
} 3.1.2 <?php
//Define a class that tracks users
class User
{
//property
public $name;
private $password, $lastLogin;
//method
public function __construct($name, $password)
{
$this->name = $name;
$this->password = $password;
$this->lastLogin = time();
$this->accesses++;
}
// Get the last visit time
function getLastLogin()
{
return(date("M d Y", $this->lastLogin));
}
}
//Create an instance of an object
$user = new User("Leon", "sdf123");
//Get the last visit time
print($user->getLastLogin() ."<br>n");
//Print username
print("$user->name<br>n");
?> When you declare a property, you don't need to specify the data type. A variable may be an integer, a string, or another object, depending on the actual situation. It is a good idea to add comments when declaring a property, marking the meaning and data type of the property.
When you declare a method, what you do is the same as defining a function outside the class. Methods and properties have their own namespaces. This means that you can safely create a method with the same name as the external class function, and the two do not conflict. For example, a class can define a method named date(). But you cannot name a method PHP keyword, such as for or while.
A class method may contain what is called type hint in PHP. Type hint is the name of another class that passes parameters to the method. If your script calls a method and passes a variable that is not an instance of the class, PHP will generate a "fatal error". You may not give type hint to other types, like integers, strings, or booleans. When writing, it is still controversial whether type hint should contain array types.
Type hint is a shortcut to test the data type of an instance of a function parameter or operator. You may always return this method. Confirm which data type you have to force a parameter to be, such as an integer. 3.2.1 Make sure that the compiled class produces only instances of Widget.
3.2.1 <?php
//Component
class Widget
{
public $name='none';
public $created=FALSE;
}
//Assembler
class Assembler
{
public function make(Widget $w)
{
print("Making $w->name<br>n");
$w->created=TRUE;
}
}
//Create a component object
$thing = new Widget;
$thing->name = 'Gadget';
//Assemble components
Assembler::make($thing);
?> In addition to the variable passing parameters, the method contains a special variable. It represents individual instances of the class. You should use this to point to the properties of the object and other methods. Some object-oriented languages assume that an unqualified variable is submitted to the local property, but any variable of the method in PHP is only within a certain scope of the method. Note the use of this variable in the constructor of the User class (3.1.2).
PHP defines an access qualifier, such as public, private and protected before the property and method declaration. In addition, you can use "static" to mark a member. You can also declare constants in the class. There will be a discussion on different access methods later in this chapter.
You can list several properties of the same access method in a row, separating them with commas. In 3.1.2, the User class has two private properties - $password and $lastLogin.