PHP13: Class Notice: This article is working on progress!
This chapter introduces the PHP class.
Currently, almost every language supports object-oriented programming, and PHP is not an exclusive programming language.
PHP does not support object-oriented programming in earlier versions, but it has been supported since PHP4, including PHP4. This series uses PHP5 as an example to describe object-oriented knowledge. The PHP4 knowledge will also be mentioned later.
In my opinion, PHP's ability to support object-oriented systems is not as deep as C ++, C #, Java, and other languages. Even so, PHP is quite comprehensive.
What are the characteristics of object orientation for each person? They must answer encapsulation, polymorphism, and inheritance. that's right.
Class
Let's start with the class. The concept of class is not mentioned, and the world knows it.
Classes in PHP can be written as follows:
Class Page
{
}
?>
It defines a simple Page class with nothing.
You can also define attributes and methods as follows:
Class Page
{
// 2 attributes
Var $ attribute1;
Var $ attribute2;
// 2 functions
Function Operation1 ()
{
}
Function Operation2 ($ param1, $ param2)
{
}
}
?>
Does PHP class have constructor and Destructor? The answer is yes, yes! Is it the same as C ++? No, it has special functions.
Construct with _ construct (); analyze with _ destruct (); (note: The front is 2 underscores)
The code is as follows:
1 2 class Page
3 {
4 // 2 attributes
5 var $ attribute1;
6 var $ attribute2;
7
8 // 2 functions
9 function Operation1 ()
10 {
11}
12
13 function Operation2 ($ param1, $ param2)
14 {
15}
16
17 // construction function
18 function _ construct ($ param)
19 {
20 $ this-> attribute1 = $ param;
21}
22
23 // destruction function
24 function _ destruct ()
25 {
26 $ this-> attribute1 = 0;
27}
28}
29?>
Does PHP support heavy load? Yes, PHP supports heavy load, so there are multiple methods to construct it.
Class instantiation
Once a class is built, we need to apply it. how can we use it? Instantiation. this is the same as other languages, so only the code is given:
1 2 class Page
3 {
4 // 2 attributes
5 var $ attribute1;
6 var $ attribute2;
7
8 // 2 functions
9 function Operation1 ()
10 {
11}
12
13 function Operation2 ($ param1, $ param2)
14 {
15}
16
17 // construction function
18 function _ construct ($ param)
19 {
20 $ this-> attribute1 = $ param;
21 echo "It will construct the attribute: {$ this-> attribute1}
";
22}
23
24 // destruction function
25 function _ destruct ()
26 {
27 $ this-> attribute1 = 0;
28}
29}
30 $ instance1 = new Page ("attribute1 in instance 1 ");
31 $ instance2 = new Page ("attribute2 in instance 2 ");
32 $ instance1 = new Page ();
33?>
The output result is:
1 It will construct the attribute: attribute1 in instance 1
2 It will construct the attribute: attribute2 in instance 2
3 It will construct the attribute:
Set or get attributes
This question is very interesting.
For example, $ attribute1 is an attribute. we can use $ this-> attribute1 to obtain or set its value.
However, to better reflect the encapsulation of classes, we still use the get or set method provided by PHP, which is similar to the functions in C. However, in PHP, two methods are provided: __get ($ name), __set ($ name, $ value) to set attributes.
Another advantage of doing so is to add your own logic according to your actual needs. if you assign values directly, you cannot do it. For example, the attribute must be a number between 1 and. what should I do? Use _ set. Let's look at the code:
1 2 class Page
3 {
4 // 2 attributes
5 var $ attribute1;
6 var $ attribute2;
7
8 // 2 functions
9 function Operation1 ()
10 {
11}
12
13 function Operation2 ($ param1, $ param2)
14 {
15}
16
17 // construction function
18 function _ construct ($ param)
19 {
20 $ this-> attribute1 = $ param;
21 echo "It will construct the attribute: {$ this-> attribute1}
";
22}
23
24 // destruction function
25 function _ destruct ()
26 {
27 $ this-> attribute1 = 0;
28}
29 function _ set ($ name, $ value)
30 {
31 if ($ name = "attribute2" & $ value> = 2 & attribute2 <= 100)
32 {
33 $ this-> attribute2 = $ value;
34}
35}
36 function _ get ($ name)
37 {
38 return $ this-> $ name;
39}
40}
41 $ instance1 = new Page ("attribute1 in instance 1 ");
42 $ instance2 = new Page ("attribute1 in instance 1 ");
43 $ instane3 = new Page ();
44 $ instane3-> attribute2 = 99;
45 echo $ instane3-> attribute2;
46?>
Class member and method visibility
Public, private, and protected. Same as C. However, it should be noted that PHP is public by default, rather than private by default in most languages. Based on this, the above example can be used. Note that if the three keywords are used, var is omitted. Let's look at an example.
1 2 class Page
3 {
4 // 2 attributes
5 public $ attribute1;
6 public $ attribute2;
7
8 // 2 functions
9 public function Operation1 ()
10 {
11}
12
13 public function Operation2 ($ param1, $ param2)
14 {
15}
16
17 // construction function
18 public function _ construct ($ param)
19 {
20 $ this-> attribute1 = $ param;
21 echo "It will construct the attribute: {$ this-> attribute1}
";
22}
23
24 // destruction function
25 public function _ destruct ()
26 {
27 $ this-> attribute1 = 0;
28}
29 public function _ set ($ name, $ value)
30 {
31 if ($ name = "attribute2" & $ value> = 2 & attribute2 <= 100)
32 {
33 $ this-> attribute2 = $ value;
34}
35}
36 public function _ get ($ name)
37 {
38 return $ this-> $ name;
39}
40}
41 $ instance1 = new Page ("attribute1 in instance 1 ");
42 $ instance2 = new Page ("attribute1 in instance 1 ");
43 $ instane3 = new Page ();
44 $ instane3-> attribute2 = 99;
45 echo $ instane3-> attribute2;
46?>
Class inheritance
Class inheritance is the same as that of C # and Java, and multi-inheritance is not supported. PHP uses extends to inherit a class, which is similar to Java.
In Java, the word final is used in PHP and has similar functions. it is used to prevent inheritance or overloading and rewrite methods.
When it comes to inheritance, does PHP support interfaces? Yes. For example:
1 2 interface IDispose
3 {
4 function Dispose ();
5}
6
7 class page extends IDispose
8 {
9 function Dispose ()
10 {
11 // Your code here.
12}
13}
14?>
New advanced functions of PHP
1. Per-class constant
It was introduced in PHP5. This common sense does not require initialization. For example:
1 2 class Math
3 {
4 const pi = 3.1415927;
5}
6 echo "Math: pi = '. Math: pi." \ n ";
7?>
It is actually the same as static variables in C ++.
2. static functions
Now that we have talked about static variables, we should also talk about static functions. Like the above idea, it requires the static keyword to be added for implementation, which is true for many languages. C #, Java, etc.
The code can be as follows:
1 2 class Shape
3 {
4 static function SquredSize ($ length)
5 {
6 return $ length * $ length;
7}
8}
9 echo "Squre:". Shape: SquredSize (10 );
10?>
3. PHP class type check
To determine whether a class is of the type of a class. It is very simple, that is, instanceof. if you have used java, you are very familiar with it, because it is the same as Java. If you use C #, it is similar to is in C.
4. Clone object
PHP provides a keyword. clone is the function. I think PHP is one of the simplest languages for me to clone objects. The code is as follows:
$ C = clone $ B
What if you want to change a feature of $ c? In this case, you need to override the _ clone method.
5. abstract class
PHP also supports abstract classes, and is very similar to C # and other languages. Here is an example.
1 2 abstract class Shape
3 {
4 abstract protected function getSquared ();
5 abstract protected function getLineCount ();
6 public function printShapeValue ()
7 {
8 echo "The squared size is: {$ this-> getSquared ()}
";
9 echo "And the number of lines is: {$ this-> getLineCount ()}
";
10}
11}
12
13 class Rectange extends Shape
14 {
15 protected function getSquared ()
16 {
17 return 12*12;
18}
19 protected function getLineCount ()
20 {
21 return 4;
22}
23}
24
25 class Triangle extends Shape
26 {
27 protected function getSquared ()
28 {
29 return 10*12;
30}
31 protected function getLineCount ()
32 {
33 return 3;
34}
35}
36
37 $ rect = new Rectange ();
38 $ rect-> printShapeValue ();
39
40 $ tri = new Triangle ();
41 $ tri-> printShapeValue ();
42?>
The output result is as follows:
The squared size is 144
And the number of lines is: 4
The squared size is 120
And the number of lines is: 3
Note the writing of abstract functions. abstract keywords. In the subclass, the override keyword is not used, but is directly rewritten. This is different from C.
6. use _ call to overload
This is a cool function that I think PHP provides. This method is used to monitor other methods in an object. If you try to call a method that does not exist in an object, the __call method will be automatically called.
For example. The code is as follows:
1 2 class Caller
3 {
4 private $ x = array (1, 2, 3 );
5
6 public function _ call ($ m, $)
7 {
8 print "Method $ m called: \ n ";
9 var_dump ($ );
10 return $ this-> x;
11}
12}
13
14 $ foo = new Caller ();
15 $ a = $ foo-& gt; test1 (1, "2", 3.4, true );
16 var_dump ($ );
17?>
The result is as follows:
Method test1 called: array (4) {[0] => int (1) [1] => string (1) "2" [2] => float (3.4) [3] => bool (true)} array (3) {[0] => int (1) [1] => int (2) [2] => int (3 )}
$ Foo-> test1 does not exist in the foo class, but after _ call is defined, it is enough.
7. use the _ autoload () method.
This function is still cool. Many developers create a PHP source file for each class definition when writing image-oriented applications. A big headache is that you have to write a long list of contained files at the beginning of each script (each class has one file. In PHP 5, we no longer need this. The _ autoload function is easy to handle. this method is automatically called when the code View uses undefined classes. By calling this function, the script engine has the last remedy chance to load the required class before an error fails. However, note that the exception thrown by the _ autoload function cannot be caught by the catch statement block and cause a fatal error.
Example:
1 2 function _ autoload ($ class_name ){
3 require_once $ class_name. '. php ';
4}
5
6 $ obj1 = new MyClass1 ();
7 $ obj2 = new MyClass2 ();
8?>
8. implement the iteration period or iteration.
It includes all visible attributes and values of the iteration class.
9. convert the type to a string (_ toString ()).
This is an application of a magic function, __tostring (). It is a bit like the toString () function in C #, but it is different for the class itself. If you implement this function in the PHP class, they call this function when printing the class. for example:
1 2 class Page
3 {
4 public function _ toString ()
5 {
6 return 'hello ';
7}
8}
9 $ page = new Page ();
10 echo $ page;
11?>
It will output:
Hello
10. reflection.
I didn't expect PHP to support reflection. If you are familiar with the reflection of C #, I don't need to mention them. If not, continue.
Reflection is the ability to search for information contained in a class or object. When such document information is unknown or not provided, you can obtain some attributes, methods, and other information of the class through reflection.
Reflection inherits some Zend classes. For more information, see The PHP Reflection class documentation.