Define a class for deep exploration of PHP5.0 object model

Source: Internet
Author: User
When you declare a class, you need to list all the variables and all functions that the object should have-called attributes and methods. The structure of a class is shown in list 1. Note that you can only declare variables or functions in braces. List 2 shows how to declare a class in one. When you declare a class, you need to list all variables and all functions that the object should have-called attributes and methods. The structure of a class is shown in list 1. Note that you can only declare variables or functions in braces. List 2 shows how to define three attributes and two methods in a class.

List 1

Class Name extends Another Class
{
Access Variable Declaration
Access Function Declaration
}


List 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 visit time
Function getLastLogin ()
{
Return (date ('m d Y', $ this-> lastLogin ));
}
}

// Create an object instance
$ User = new User ('Leon', 'sdf123 ');

// Obtain the last visit time
Print ($ user-> getLastLogin ().'
N ');

// Print the user name
Print ('$ user-> name n ');
?>


When you declare an attribute, you do not need to specify the data type. The variable may be an integer, string, or another object, depending on the actual situation. It is a good idea to add a comment when declaring an attribute. it indicates the meaning and data type of the attribute.

When you declare a method, what you do is the same as defining a function outside the class. Methods and attributes have their own namespaces. This means you can safely create a method with the same name as an external function of the class, and the two do not conflict. For example, a class can define a method named date. However, you cannot name a method as the keyword of PHP, such as for or while.

Class methods may contain the so-called type hint in PHP. type hint is the name of the class passed to the method by another parameter. 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 give type hint to other types, such as integer, string, or boolean value. whether the type hint should contain the array type remains controversial during writing.

Type hint is a shortcut to test the data Type of an instance of function parameters or operators. you may always return this method. confirm which data type you want to force a parameter to be, such as an integer. list 3: Make sure that the compiling class only generates widgets.

List 3

// Component
Class Widget
{
Public $ name = 'none ';
Public $ created = FALSE;
}

// Assembler
Class cycler
{
Public function make (Widget $ w)
{
Print ('making $ w-> name
N ');
$ W-> created = TRUE;
}
}

// Create a Component Object
$ Thing = new Widget;
$ Thing-> name = 'gadget ';

// Assemble components
Else ER: make ($ thing );
?>


In addition to passing parameter variables, the method contains a special variable. it represents an individual instance of the class. you should use this to point to the object attributes and other methods. some object-oriented languages assume that a variable of different cells is submitted to the local attribute, but any variable of the method in PHP is only within the scope of the method. pay attention to the application of this variable in the structure function of the User class (3.1.2 ).

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.