Usage of the phpclass class

Source: Internet
Author: User
Tags class manager getv
The following is a detailed summary of the usage of the class in php. For more information, see

The following is a detailed summary of the usage of the class in php. For more information, see

1. Structure and call (instantiation ):

Class className {}, call: $ obj = new className (); when the class has a constructor, you should also input parameters. For example, $ obj = new className ($ v, $ v2 ...);

2. constructor and destructor:

1. constructor for initialization: Use _ construct (), which can contain parameters.

2. But the Destructor cannot contain parameters (used to perform some operations or functions before a class is sold ). The Destructor uses _ destruct () as the name. At the end of the script execution, the objects in the memory will be sold out, so you do not need to analyze the function. However, for some objects such as cookies, you should use this function to sell out.

Knowledge Point: constructor is also provided in PHP4, but a class method with the same name as the class is used. PHP5 is still compatible with this method. When a class does not contain _ construct, it will look for methods with the same name as the class. If it is found, it is considered as a constructor, as follows:

The Code is as follows:


Class test
{Var $ B;
Function test () {$ this-> B = 5 ;}
Function addab ($ c) {return $ this-> B + $ c ;}
}
$ A = new test (); echo $ a-> addab (4); // return 9


3. PHP does not automatically call the constructor of the parent class (the constructor overload is not supported). It must be explicitly called using the parent keyword.

The Code is as follows:


Class employee {
Function _ construct ()....
}
Class Manager extents Employee {
Function _ construct (){
Parent: _ construct ();
Echo 'the parent class constructor of this subclass has been called! ';
}
}


You can also call constructors of other classes that are irrelevant to the instance. You only need to add the class name before _ construct. For example:
OtherClassName ::__ construct ();

Main family members of the class: attributes, methods, constants, and static members

Iii. class attributes:
There are two ways to assign values to the attributes of a class.
1. Use the public keyword in the public scope.
2. Use _ set () and _ get () to assign values respectively. The former is called setter or mutator ), the latter is called an access method or a getter ). This method is recommended. Advantages:
A. data verification can be performed in A unified manner in _ set.
B. It is convenient to manage attributes in a unified manner.

Note:

First :__ set () and _ get () Act only on private attributes. For attributes defined by public, both of them are lazy, as shown below:

The Code is as follows:


Class test {
Protected $ a = 9, $ B = 2, $ c;
Public $ d;
Function _ set ($ n, $ v) {$ this-> $ n = $ v + 2 ;}
Function _ get ($ name) {return $ this-> $ name + 2 ;}
}
$ A = new test ();
$ A-> B = 5; echo"
"; Echo $ a-> B;


For $ a, $ B, and $ c, the instance is filtered and returned through _ set and _ get. For $ d, the instance does not work. For example, $ a-> d = 5, and then return 5.

Second :__ set ($ n, $ v) must contain two parameters. But _ get ($ n) can have only one parameter. Instance:

The Code is as follows:


Class test {
Private $ a = 5, $ B = 6, $ c;
Function _ set ($ n, $ v)
{
If ($ n = 'A' & $ n> 0)
$ This-> $ n = $ v;
Else
$ This-> $ n = $ v + 2;
}
Function _ get ($ name)
{
Return $ this-> $ name; // if it is changed to return $ this-> $ name + $ this-> addab (); for example, call the value of, the actual returned value is a + B. The default value is 5 + 5 + 6 = 16.
}
Function addab ()
{Return $ this-> a + $ this-> B ;}
}
$ E = new test ();
$ E-> a = 11; // Note: $ this-> $ n is used inside the class to write the variable, but $ e-> a is used for external instances.
$ E-> B = 12; // get 14
$ E-> k = 22;


The attributes of a class can be freely expanded. Whether or not _ set is used for k in the preceding example, you can use $ e-> newProperty = xx after an instance is created; directly create an attribute, but it is not recommended to do so.

Iv. Class methods:
It can be understood as a function in the class.

Call:

1. Internal call: You can use $ this-> Fanname (); or $ this-> addab () or test: addab ();

2. When calling an instance, use $ e-> addab. If the $ this keyword is not used in this method, in the example above:
Function addab () {return $ this-> a + $ this-> B ;}
Change to: function addab () {return 25;}. If this method is called on an external instance, you can also use "$ e: addab ();" or "test: addab ();"

5. constants of the class:
If the attributes of a class are interpreted as variables in the class, the constants and variables of the class are different. The definition method is as follows:

The Code is as follows:


Class test {
Private $;
Const PI = '3. 14 ′;
.....
// Call the above constant in the class using two methods, "$ this: PI", or "Class Name: PI". Here is test: PI, as shown below:
Function getvalue (){
Return $ this-> a * $ this: PI; // or $ this-> a * test: PI. You can use the this keyword or class name, however, both use double colons.
}
}
$ E = new test ();
$ E-> PI = 5; // Note: Here-> is used to create an attribute named PI, rather than changing the value of the PI constant in the class.
Echo $ e: PI; // This is the constant of the call class.


Constants can only be called with double colons. And its value cannot be changed.
There are also two methods to call class constants after class external instantiation. Method:
"$ E: PI" or "test: PI", both use colons. The difference is that the this keyword cannot be used externally, but only the Instance name can be used, but the class name is :: PI is generic.

6. Static members of a class (static attributes or static methods ):

If you want to create a field or method shared by all classes of the instance. You must use static members. There are two features:

1. The static member is a communist who calls all the instances of the class in the script, but cannot call the instance by the specific instance name of the class. Instead, the class name is used outside the class:: $ member name. The class is called using the "self: $ member name.

2. When each new instance is created, the static member will re-calculate from the last value of the last instance, instead of the initial value in the class.

3. The value of a static member defined by public can be changed externally. Private.

The Code is as follows:


Class test {
Public static $ v = 0;
Function _ construct () {self: $ v ++ ;}
Static function getV () {return self: $ v ;}
}
$ A = new test ();
Echo test: getV (); // return 1
$ B = new test ();
Echo test: getV (); // return 2
Test: $ v = 8; // The value of the static member is changed due to the public defined member.
$ C = new test ();
Echo test: getV (); // return 9


VII. Keywords:
(1) this Keyword: used internally to refer to the class itself. To access attributes, methods, or constants, such as $ this-> attribute name or method name. $ This: constant name. This can also be used in subclass of this class to refer to its own attributes or methods.

(2) Double colon ":" Keyword: Used to call constants and static members.

(3) self Keyword: static members can be called in combination with double colons in the class, such as self ::$ staticVar... in the class, $ this cannot be used to call static members.

(4) _ toString (): Use _ toString () in the class to convert the class into a string and print the class. It is of little use, for example:

The Code is as follows:

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.