Section 3-define a class
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
<? Php
// 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 (). "<br> ");