Chapter 2 object-oriented tools

Source: Internet
Author: User

Learning points:
1. The magic of OOP
2. Class Functions and object functions
3. OOP reflection API

 

PHP supports object-oriented programming through language structures such as classes and methods. At the same time, through object-related functions and
Built-in classes provide extensive support for Object-Oriented applications.

 

I. The magic of OOP

PHP introduces the _ autoload () built-in method to automatically include class files. _ Autoload () should be written as a single parameter
Number method. When the PHP engine attempts to instantiate an unknown class, it will call the _ autoload () method and
Name is passed to it as a string parameter.

function __autoload($_className) {    require $_className . '.class.php';}$demo = new Computer ();

PHP uses the _ call () built-in method to block Errors generated when an object calls a method. When an object does not exist
The _ call () method is automatically called.

Private function _ call ($ _ methodName, $ args) {echo $ _ methodName. 'method does not exist'; print_r ($ args);} $ computer-> go ('my', 1, '');

PHP uses the _ toString () built-in method to print object references. Objects without _ toString () are generated.
An error occurs. when an object is printed, the _ toString () method is automatically called.

Class Computer {private function _ toString () {return 'print object' ;}} echo new Computer ();

PHP can define a built-in _ clone () method in the class to adjust the object cloning behavior. When an object is
The _ clone () method is automatically executed during the long operation, and the Copied object can be adjusted in the method body.

class Computer {    public $_name;    public function __clone() {        $this->_name = 'ibm';    }}$computer1 = new Computer ();$computer1->_name = 'dell';$computer2 = clone $computer1;echo $computer2->_name;

 

Ii. Class Functions and object functions

PHP provides a series of powerful functions to detect classes and objects. So that you can know that the system is running in a third-party system.
Which is used.

1. The class_exists () function accepts the string representing the class, checks and returns a Boolean value. Returns true if the class exists,
Otherwise, false is returned.

echo class_exists('Computer');

2. The get_class () function obtains the class name of an object. If it is not an object, false is returned.

echo get_class($computer);

3. The get_class_methods () function obtains the method (public) in the class and returns it as an array.

print_r(get_class_methods($computer));

4. The get_class_vars () function gets the fields (public) in the class and returns them as arrays.

print_r(get_class_vars('Computer'));

5. The get_parent_class () function gets the parent class of the subclass. If no value is returned, false is returned.

echo get_parent_class('NoteComputer');

6. The interface_exists () function determines whether an interface exists. If yes, true is returned. Otherwise, false is returned.

echo interface_exists('Computer');

7. When the is_a () function determines whether the object is a class or whether it is a class parent class, true is returned; otherwise, false is returned.

echo is_a($computer,'Computer');

8. The is_subclass_of () function determines whether the object is a subclass of the class. It returns true; otherwise, false.

echo is_subclass_of($notecomputer,'Computer');

9. The method_exists () function determines whether the object's method exists. True is returned; otherwise, false is returned.

echo method_exists($computer,'_run');

 

Iii. OOP reflection API

PHP5 classes and object functions do not tell us everything inside the class, but only report their common
Member. To fully understand a class, you must know its private and protected members, and the parameters expected by the method.
To do this, use the reflection API.

1. Get the dumping information of the reflection API

$rc = new ReflectionClass('Computer');Reflection::export($rc);

2. obtain information about the PHP built-in class library.

Reflection::export(new ReflectionClass('Reflection'));

3. Get an element in the class

$_rc = new ReflectionClass('Computer');echo $_rc->getFileName();echo $_rc->getName();

Note: This article is a video tutorial from Li Yanhui PHP. This article is intended for communication purposes only and cannot be used for commercial purposes. Otherwise, the consequences will be borne by you.

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.