Php. Oop

Source: Internet
Author: User
Tags print object

Learning Essentials:

The Magic method of 1.OOP

2. Class functions and Object functions

Reflection API for 3.OOP

PHP supports object-oriented programming through language constructs such as classes and methods. It also provides extensive support for object-oriented objects through object-related functions and built-in classes.

A The Magic method of OOP
PHP introduces the __autoload () built-in method to automatically include class files. __autoload () should be written as a method of a single parameter. When the PHP engine encounters an operation that attempts to instantiate an unknown class, it calls the __autoload () method and passes the class name as a string parameter to it.
function __autoload ($_classname) {
Require $_classname. Class.php ';
}
$demo = new computer ();//Auto-trigger __autoload () method, but must exist __autoload () this method

PHP uses the __call () built-in method to mask errors that occur when an object calls a method. The __call () method is called automatically when an object calls a method that does not exist.
Private Function __call ($_methodname, $args) {
echo $_methodname. ' method does not exist ';
Print_r ($args);
}
$computer->go (' I ', 1, ' know '),//will automatically trigger the __call method, also must exist __autoload () this method

PHP uses the __tostring () built-in method to print a reference to an object. An object that does not use __tostring () generates an error that automatically calls the __tostring () method when the object is printed.
Class Computer {
Private Function __tostring () {
Return ' Print object ';
}
}
echo New Computer ()///will automatically trigger the __tostring () method inside the computer class, if there is no __tostring method inside the computer will produce an error

PHP can define a __clone () built-in method in the class to adjust the cloning behavior of the object. The __clone () method is automatically executed when an object is cloned, and the copied object can be adjusted within its method body.
Class Computer {
Public $_name;
Public Function __clone () {
$this->_name = ' 123 ';
}
}
$computer 1 = new computer ();
$computer 1->_name = ' dell ';
$computer 2 = clone $computer 1;//$computer 2 instance automatically executes the __clone method internally
echo $computer 2->_name;//print out 123
echo $computer 1->_name;//print out Dell

Two class functions and Object functions
PHP provides a series of powerful functions to detect classes and objects. So that in a third-party system, the runtime knows which is being used.
The 1.class_exists () function accepts a string representing the class and checks for and returns a Boolean value. Returns True if the class exists, otherwise false is returned.
echo class_exists (' computer ');
The 2.get_class () function gets the class name of the object and returns False if it is not an object.
echo Get_class ($computer);
The 3.get_class_methods () function Gets the method (public) in the class and returns it as an array.
Print_r (Get_class_methods ($computer));
The 4.get_class_vars () function gets the field (public) in the class and returns it as an array
Print_r (Get_class_vars (' computer '));
The 5.get_parent_class () function gets the parent class of the subclass if it does not return false.
echo get_parent_class (' Notecomputer ');
The 6.interface_exists () function determines whether the interface exists and returns true if present, otherwise false.
echo interface_exists (' computer ');
The 7.is_a () function returns ture if the object is a class or is a parent class of this class, otherwise returns FALSE.
Echo is_a ($computer, ' computer ');
The 8.is_subclass_of () function determines whether the object is a subclass of the class, returns Ture, or returns false.
Echo is_subclass_of ($notecomputer, ' computer ');
The 9.method_exists () function determines whether an object's method exists, returns ture, or returns false.
Echo method_exists ($computer, ' _run ');

Three The Reflection API for OOP
The class and object functions of PHP5 do not tell us everything inside the class, but simply report their public members. To fully understand a class, you need to know its private and protected members, as well as the parameters that its methods expect.
For this, use the reflection API.
1. Get dump information for the reflection API
$RC = new Reflectionclass (' computer ');
Reflection::export ($RC);
2. Get information about the PHP built-in class library
Reflection::export (New Reflectionclass (' Reflection '));
3. Get an element in a class
$_RC = new Reflectionclass (' computer ');
Echo $_rc->getfilename ();
Echo $_rc->getname ();

Php. Oop

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.