PHP 5.0 object model deep exploration defines a class

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

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.