Section 3 -- define a class -- ClassesandObjectsinPHP5 [3] _ PHP Tutorial

Source: Internet
Author: User
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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.