Learning classes in php5

Source: Internet
Author: User

Copy codeThe Code is as follows:
<? Php
Class TEST
{
Const NAME = 'value'; // constant
Public $ name = 'value'; // attribute
Public function name () // Method
{
Echo 'value ';
}
}
?>


In this case, attributes and methods can use three different keywords: public, protected, and private to further differentiate the scope of the attributes and methods, for attributes and methods with private keywords, only the methods in the class can be called. For attributes and methods with the protected keyword, methods In the parent class and subclass can also be called. attributes and methods with the public keyword can be called from objects after instantiation, the biggest benefit of doing so adds descriptive features to all attributes and methods, making it easier to organize and organize the code structure. The const keyword is skipped first, and will be discussed together with the static keyword later.

The static keyword is another type of keyword different from public, protected, and private (so it can be used together with public, protected, and private ):

Copy codeThe Code is as follows:
<? Php
Class TEST
{
Public static function name ()
{
Echo 'value ';
}
}
?>

A method with the static keyword can be called directly through the ":" symbol without class instantiation. It can be used with the public, protected, and private symbols to differentiate the call permissions, however, it is usually used together with the public. The constant keyword const mentioned above should be of the public static type. Therefore, constants can only be called in the form of self: NAME, TEST: NAME, the following methods, such as _ construct ,__ destruct, are static.

The structure of the class. The last two keywords are abstract and final. abstract keywords indicate that the class must be overwritten by its subclass, the final keyword indicates that the class must not be overwritten by its subclass. The functions of these two keywords are the opposite. abstract methods are called abstract methods and classes with abstract methods, become an abstract class, which will be introduced later.

Class usage:

There are two main methods to use the class. One is to use the new keyword, and the other is to use the ":" symbol:

PHP code
Copy codeThe Code is as follows:
<? Php
Class TEST
{
Public static function name ()
{
Echo 'value ';
}
}
// Method 1: Use the new Keyword
$ Test = new TEST;
$ Test-> name ();

// Method 2: Use the ":" symbol
TEST: name ();
?>


(1): The new Keyword is used for instantiation. The above $ test is an object generated by the TEST class instantiation. $ test-> name () this is called the name method of the $ test object.
(2): when using the new keyword to use the class, you can use $ this to refer to the class itself.
(3): the prerequisite for using the ":" symbol is that the method must have a static keyword. When the new keyword is used, the called method, public keywords MUST be included. (If a method does not contain any keyword in public, protected, or private, the default value is public)
(4): the same class can be instance into multiple different objects through the new Keyword, but they are isolated from each other. When the ":" symbol is used, methods are shared among multiple times:

PHP code
Copy codeThe Code is as follows:
<? Php
Class TEST1
{
Public $ name = 0;
Public function name ()
{
$ This-> name = $ this-> name + 1;
}
}

$ Test1 = new TEST1;
$ Test2 = new TEST1;
$ Test1-> name (); // $ name1 = 1
$ Test2-> name (); // $ name1 = 1

/*--------------------------------------------*/

Class TEST2
{
Public static $ name = 0;
Public static function name ()
{
TEST2: $ name = TEST2: $ name + 1;

}
}
TEST2: name (); // $ name = 1
TEST2: name (); // $ name = 2
?>
Class relationship:

The relationships between classes mainly include abstraction, interfaces, and inheritance:

PHP code
Copy codeThe Code is as follows:
<? Php
Abstract class TEST1 // Abstraction
{
Abstract public function name1 ();
Public function name2 ()
{

}
}

Class TEST2 extends TEST1 implements TEST3 // inherit
{
Public function name1 ()
{

}
}

Interface TEST3 // interface
{
Public function name2 ();
}
?>


(1) classes with abstract keywords are abstract classes. Methods with abstract keywords are abstract methods. abstract methods in abstract classes must be overwritten in subclasses.
(2) A class with the interface keyword is an interface. An interface cannot implement any method. All methods in the interface must be overwritten in the subclass.
(3) The words classA extends classB or classA implements classB are inheritance. extends indicates inheritance of another class, implements indicates inheritance of another interface, and only one extends class at a time, however, multiple implements interfaces are supported.
(4) abstract classes, interfaces, and methods that are ultimately inherited and implemented must all be public.

During the inheritance process, the subclass will overwrite the method of the parent class with the same name. If you need to call the method of the parent class in the subclass, you can add the ": symbol call:

PHP code
Copy codeThe Code is as follows:
<? Php
Class TEST1 extends TEST2
{
Public function name ()
{
Echo parent: name2 ();
Echo TEST2: name2 ();
}
}
Class TEST2
{
Public function name2 ()
{
Echo 'value2 ';
}
}
$ Test = new TEST1;
$ Test-> name ();
?>


Here I will explain the role of the ":" method in the class. One function is to call constants without instantiation (in fact, it can be understood as static ), static attributes and methods, and a convenient call Channel is established within the class through self, parent, and class name.

The relationship between objects, mainly "=" equals to, "=" equals to, not equal to and clone:

PHP code
<? Php
Class TEST
{
Public function name ()
{

}
}

$ Test1 = new TEST;
$ Test2 = new TEST;
$ Test3 = $ test1;
Echo $ test1 = $ test2? True: false; // true
Echo $ test1 = $ test3? True: false; // true
Echo $ test2 = $ test3? True: false; // true
Echo $ test1 ===$ test2? True: false; // false
Echo $ test1 ===$ test3? True: false; // true
Echo $ test2 ===$ test3? True: false; // false
?>

(1) As long as the two classes have the same attributes and methods, they are equal to "=.
(2) The two classes must point to the same object to be "=" equals to all.

Clone is special. In the preceding example, $ test3 = $ test1 does not give $ test3 a copy of $ test1 object, but points $ test3 to $ test1, to get a copy of $ test1, you must use the clone Keyword:

PHP code
Copy codeThe Code is as follows:
<? Php
$ Test3 = clone $ test1;
?>


Class HOOK:

_ Autoload:
It is a function name and the only hook used outside the class. when instantiating an object, if the class is not preloaded, the hook is called.

_ Construct
When a class is used as an instance, the called Hook can perform initialization operations.

_ Destruct
The called hook when the class is destroyed.

_ Call
When an object tries to call a non-existent method, the called Hook

_ Sleep
When you use the serialize () function to perform sequential calls on a class, this hook is called.

_ Wakeup
When the unserialize () function is used to perform the anti-sequential talk operation on a class, this hook is called.

_ ToString
This hook is called when an object is converted to a string (for example, echo)

_ Set_state
When you call the var_export () function to operate a class, this hook is called.

_ Clone
This hook is called when the clone keyword is used to copy a class.

_ Get
This hook is called when obtaining the attribute value of a class.

_ Set
This hook is called when setting the attribute value of a class.

_ Isset
When the isset () function is used to determine the attribute values in the class, this hook is called.

_ Unset
This hook is called when the unset () function is used to destroy a property value.

TIPS:

When using an instance class, you can use this form to pass parameters to the _ construct HOOK:

PHP code
Copy codeThe Code is as follows:
<? Php
Class TEST
{
Public function _ construct ($ para)
{
Echo $ para;
}
}

$ Test = new TEST ('value'); // display value
?>


The foreach () function can be used to traverse attributes in a class or object. When traversing, it first determines the public, protected, and private attributes and displays them:

PHP code
Copy codeThe Code is as follows:
<? Php
Class TEST
{
Public $ property1 = 'value1 ';
Public $ property2 = 'value2 ';
Public $ property3 = 'value3 ';

Public function name ()
{
Foreach ($ this as $ key => $ value)
{
Print "$ key => $ value \ n ";
}
}
}

$ Test = new TEST;
Foreach ($ test as $ key => $ value)
{
Print "$ key => $ value \ n ";
}
$ Test-> name ();
?>

When passing parameters to methods in the class, you can forcibly determine the parameters. Here, you can only determine the array and object:

PHP code
Copy codeThe Code is as follows:
<? Php
Class TEST1
{
Public function name (TEST2 $ para)
{

}
}

Class TEST2
{

}

$ Test2 = new TEST2;
$ Test1 = new TEST1;

$ Test1-> name ('value'); // an error is returned because this parameter must be an object after TEST2 is instantiated.
$ Test1-> name ($ test1); // No error is reported
?>


Compatible with php4 Syntax:

Php5 classes are compatible with php4, And the syntaxes in these php4 era are inherited, but it is not recommended to use them in the php5 environment.

(1) using the var default attribute will be automatically converted to public.

(2) If the class name is used as the constructor and the _ construct constructor is not used, a function with the same class name will be searched as the constructor.

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.