When you declare a class, do you need to list all the variables and all the functions that the object should have? These are called properties and methods. Figure 1 shows the structure of a class. Note that you can only declare variables within curly brackets ({}). Or a function. Figure 2 shows how to define three properties and two methods in a class.
class Name extends Another Class
{
Access Variable Declaration
Access Function Declaration
}
name = $name;
$this->password = $password;
$this->lastLogin = time();
$this->accesses++;
}
// Get the time of the last visit
function getLastLogin()
{
return(date("M d Y", $this->lastLogin));
}
}
//Create an instance of an object
$user = new User("Leon", "sdf123");
//Get the time of the last visit
print($user->getLastLogin() ."n");
//Print user name
print("$user->namen");
?>
When you declare a property, you do not need to specify the data type. The variable may be an integer, a string, or another object, depending on the situation. It is a good idea to add comments when declaring properties, marking the meaning of the property. and data types.
When you declare a method, you are doing the same as defining a function outside the class. Methods and properties have their own namespaces. This means you can safely create a function with the same name as outside the class. Methods, the two will not conflict. For example, a class can define a method named date(). But you cannot name a method as a PHP keyword, such as for or while.
Class methods may contain so-called date() in PHP type hint. Type hint is the name of another class that is passed as an argument to a 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 have given it to other Types give type hints, like integers, strings, or booleans. At the time of writing, it was controversial whether type hints should include array types.
Type hints are a shortcut for testing the data type of function parameters or operator instances. You may always return this method. Make sure you force a parameter to be a data type, such as an integer. Figure 3 Ensure that the compiled class only produces Widgets Example of
<?php
//component
classWidget
{
public $name='none';
public $created=FALSE;
}
//Assembler
classAssembler
{
public function make(Widget $w)
{
print("Making $w->namen");
$w->created=TRUE;
}
}
//Create a component object
$thing = new Widget;
$thing->name = 'Gadget';
//Assembly component
Assembler::make($thing);
?>
In addition to the variables passed to the parameters, methods contain a special variable. It represents an individual instance of the class. You should use this to point to the object's properties and other methods. Some object-oriented languages assume that an unqualified variable is submitted to the local Attribute, but in PHP any variable of a method is only within a certain scope of the method. Note the use of this variable in the constructor of the User class in Figure 2.
PHP defines an access qualifier before properties and methods are declared, such as public, private, and protected. In addition, you can mark a member with "static". You can also declare constants in the class. Different access methods will be discussed later in this chapter related discussions.
You can list several properties with the same access method on one line, separating them with commas. In Figure 2, the User class has two private properties-$password and $lastLogin.