PHP class and object full parsing (1) _ PHP Tutorial

Source: Internet
Author: User
PHP class and object resolution (1 ). PHP class and object full resolution (1) 1. class and object: the actual existence of each object in this class of things. $ AnewUser (); $ a reference after instantiation: php alias. two different PHP classes and objects are completely parsed (1)
1. classes and objects

Object: the entity that actually exists in each object of this type of thing. $ A = new User (); $ a after instantiation

Reference: php alias. two different variable names point to the same content.

Encapsulation: organizes object attributes and methods into a class (logical unit ).

Inheritance: creates a new class based on the original class to reuse the code;

Polymorphism: assign a pointer of the subclass type to the pointer of the parent class.

-------------------------------------

2. automatically load objects:

The special _ autoload function is automatically loaded. This function is automatically called when a class not defined in the script is referenced.

1 [php] view plaincopyprint?

2 function _ autoload ($ class ){

3 require_once ("classes/$ class. class. php ");

4}

Why use _ autoload?

1. first, I don't know where this class of files are stored,

2. I don't know when to use this file.

3. in particular, if there are too many project files, it is impossible for each file to write a long string of require at the beginning...

Replace 1

Require_once ("classes/Books. class. php ");

Require_once ("classes/Employees. class. php ");

Require_once ("classes/Events. class. php ");

Require_once ("classes/Patrons. class. php ");

Zend recommends the most popular method to include a path in a file name. For example:

1 [php] view plaincopyprint?

2

3 view sourceprint?

4 // Main. class

5

6 function _ autoload ($ class_name ){

7 $ path = str_replace ('_', DIRECTORY_SEPARATOR, $ class_name );

8 require_once $ path. '. php ';

9}

Temp = new Main_Super_Class ();

All underlines are replaced with delimiters in the path. in the preceding example, the Main/Super/Class. php file is used.

Disadvantages:

In the coding process, you must clearly know where the code file should be located,

In addition, because the file path is hardcoded in the class name, if you need to modify the structure of the folder, we must manually modify all the class names.

If you are in a development environment and are not very concerned about the speed, it is very convenient to use the 'include all' method.

By placing all class files in one or more specific folders, and then searching and loading through traversal.

For example

$ Arr = array (

'Project/class ',

'Project/Classes/Children ',

'Project/interfaces'

);

Foreach ($ arr as $ dir ){

$ Dir_list = opendir ($ dir );

While ($ file = readdir ($ dir_list )){

$ Path = $ dir. DIRECTORY_SEPARATOR. $ file;

If (in_array ($ file, array ('.', '..') | is_dir ($ path ))

Continue;

If (strpos ($ file, ". class. php "))

Require_once $ path;

}

}

?>

Another method is to establish an associated configuration file between the class file and its location, for example:

View sourceprint?

// Configuration. php

Array_of_associations = array (

'Mainsuperclass' = 'C:/Main/Super/Class. php ',

'Mainpoorclass' = 'C:/blablabla/gy. php'

);

Called file

Require 'autoload _ generated. php ';

Function _ autoload ($ className ){

Global $ autoload_list;

Require_once $ autoload_list [$ className];

}

$ X = new ();

?>

------------------------------------------------

3. constructor and Destructor

PHP constructor _ construct () allows you to execute constructor before instantiating a class.

Constructor is a special method in the class. When you use the new operator to create an instance of a class, the constructor will automatically call it. its name must be _ construct ().

(Only one constructor can be declared in a class. Instead, the constructor is called only once every time an object is created,

So it is usually used to execute some useful initialization tasks. This method has no return value .)

Purpose: Initialize an object when creating an object.

Constructor parent ::__ construct () for sub-classes ().

Destructor: _ destruct () definition: special Inner member functions. they have no return type, no parameters, cannot be called at will, and are not overloaded;

It is only at the end of the class object life that the system automatically calls to release the resources allocated in the constructor.

The constructor corresponds to the constructor. The constructor allows you to perform operations or complete some functions before destroying a class, such as disabling files and releasing result sets.

The Destructor cannot contain any parameters, and its name must be _ destruct ().

Purpose: clear up the aftermath. for example, when creating an object, use new to open up a memory space. before exiting, use the destructor to release the resources allocated in the constructor.

Example:

Class Person {

Public $ name;

Public $ age;

// Define a constructor initialization value assignment

Public function _ construct ($ name, $ age ){

$ This-> name = $ name;

$ This-> age = $ age;

}

Public function say (){

Echo "my name is:". $ this-> name ."
";

Echo "my age is:". $ this-> age;

}

// Destructor

Function _ destruct ()

{

Echo "goodbye:". $ this-> name;

}

}

$ P1 = new Person ("ren", 25 );

$ P1-> say ();

---------------------------------------------------------------

4. access control

Access control for properties or methods is implemented by adding the public, protected, or private keywords in the front.

The class members defined by public can be accessed anywhere;

The class member defined by protected can be accessed by its subclass and parent class (of course, the class where the member is located can also be accessed );

Private-defined class members can only be accessed by their classes.

Access control for class members

All class members must use the public, protected, or private keywords for definition.

Method access control

Methods in the class must be defined using the public, protected, or private keywords. If these keywords are not set, the method is set to the default public.

Example:

Class MyClass

{

Public $ public = 'public ';

Protected $ protected = 'protected ';

Private $ private = 'private ';

Function printHello ()

{

Echo $ this-> public;

Echo $ this-> protected;

Echo $ this-> private;

}

}

$ Obj = new MyClass ();

Echo $ obj-> public; // This line can be executed normally.

Echo $ obj-> protected; // This line produces a fatal error

Echo $ obj-> private; // This line also produces a fatal error.

$ Obj-> printHello (); // output Public, Protected, and Private

-------------------------------------------------------------

5. object inheritance

Inheritance definition: creates a new class based on the original class to reuse the code;

--------------------------------------

Overwriting is used in object inheritance.

Overload is a method with different parameters in the same method name in a single object.

--------------------------------------

Inheriting a well-known programming feature, PHP's object model also uses inheritance. Inheritance will affect the relationship between classes and objects.

For example, when a class is extended, the subclass inherits all the public and protection methods of the parent class. However, the subclass method overwrites the method of the parent class.

Inheritance is very useful for the design and abstraction of functions, and there is no need to re-write these common functions when new features are added to similar objects.

Class Person {

Public $ name;

Public $ age;

Function say (){

Echo "my name is:". $ this-> name ."
";

Echo "my age is:". $ this-> age;

}

}

// Class inheritance

Class Student extends Person {

Var $ school; // attributes of the student's school

Function study (){

Echo "my name is:". $ this-> name ."
";

Echo "my shool is:". $ this-> school;

}

}

$ T1 = new Student ();

$ T1-> name = "zhangsan ";

$ T1-> school = "beijindaxue ";

$ T1-> study ();

--------------------------------------------

6. range resolution operator (::)

The range resolution operator (also known as Paamayim nekudow.im) or, more simply, a pair of colons, can be used to access static members, methods, and constants, and can also be used to override members and methods in the class.

When you access these static members, methods, and constants outside the class, you must use the class name.

The special keywords self and parent are used to access members or methods within the class.

Note:

When a subclass overrides the methods in its parent class, PHP will no longer execute the override methods in its parent class until these methods are called in the subclass.

Example:

Class OtherClass extends MyClass

{

Public static $ my_static = 'static var ';

Public static function doubleColon (){

Echo parent: CONST_VALUE. "\ n ";

Echo self: $ my_static. "\ n ";

}

}

OtherClass: doubleColon ();

?>

Http://www.bkjia.com/PHPjc/934463.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/934463.htmlTechArticlePHP class and object full resolution (a) 1. class and object: the actual existence of this class of things in each object of the individual. $ A = new User (); $ a reference after instantiation: php alias, two different...

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.