Section three-Defining a class-Classes and Objects in PHP5 [3]_php tutorial

Source: Internet
Author: User
Section three-Defining a class
When you declare a class, you need to list all the 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 You can only declare variables or functions within curly 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
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++;
}
Gets the last access time
function Getlastlogin ()
{
Return (Date ("M D Y", $this->lastlogin));
}
}
Create an instance of an object
$user = New User ("Leon", "sdf123");
Gets the last access time
Print ($user->getlastlogin (). "
");

http://www.bkjia.com/PHPjc/445267.html www.bkjia.com true http://www.bkjia.com/PHPjc/445267.html techarticle section III-Defining a class when you declare a class, you need to list all the variables that the object should have and all of the functions are called properties and methods. The composition of a class is shown in 3.1.1. Note in the large enclosed ...

  • 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.