Section 3 -- define a class -- ClassesandObjectsinPHP5 [3]. Section 3-defines a class. When you declare a class, you need to list all variables and all functions that an object should have. 3.1.1 shows the composition of a class. note: define a class in section 3.
When you declare a class, you need to list all variables and all functions of the object-called attributes and methods. 3.1.1 shows the composition of a class. note that you can only declare variables or functions in braces. 3.1.2 shows how to define three attributes and two methods in a class.
3.1.1
Class Name extends Another Class
{
Access Variable Declaration
Access Function Declaration
}
3.1.2
// Define a class for tracking users
Class User
{
// Attributes
Public $ name;
Private $ password, $ lastLogin;
// Method
Public function _ construct ($ name, $ password)
{
$ This-> name = $ name;
$ This-> password = $ password;
$ This-> lastLogin = time ();
$ This-> accesses ++;
}
// Obtain the last access time
Function getLastLogin ()
{
Return (date ("M d Y", $ this-> lastLogin ));
}
}
// Create an object instance
$ User = new User ("Leon", "sdf123 ");
// Obtain the last access time
Print ($ user-> getLastLogin ()."
");
When you declare a class, you need to list all variables of the object and all functions called attributes and methods. 3.1.1 shows the composition of a class...