PhP5 object model [3]-define a class

Source: Internet
Author: User
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 ()."
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 a PHP keyword, 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. at the time of writing, whether the type hint should contain the array type is still controversial.

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. 3.2.1 make sure that the compilation class only generates widgets.

3.2.1

// 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 an unqualified variable is submitted to the local attribute, but any variable of the method in PHP is only within a certain scope of the method. note the usage of this variable in the user class Constructor (3.1.2 ).

PHP defines an access restriction before the attribute and method declaration, such as public, private, and protected. in addition, you can use "static" to mark a member. you can also declare constants in the class. this chapter will discuss different access methods later.

You can list several attributes of the same access method in a row and separate them with commas. In 3.1.2, the user class has two private attributes: $ password and $ lastlogin.

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.