PHP Object-oriented tools

Source: Internet
Author: User

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

The Magic method of OOP

__autoload () Auto-load class method

General introduction of class file Problems 1. If you want to include multiple class files, are they all introduced? 2. the introduction of multiple class files, if some are not used, will create a waste 3. introduction of the class file, may be omitted, for example, to create an object, and the object's class file is not imported. will produce an error PHP introduced 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 . that is, when you try to instantiate a class, the __autoload () method is invoked to load the class file hint with the intent instantiation: Note The file command specification at this point, a class file  to suffix end of. class.php
#file name Computer.class.phptip: Here's computer class name capitalized vim Computer.class.phpclasscomputer{ Public $_name= "Lenovo";  Public function__construct () {Echo"<meta charset=utf8>"; }     Public function_run () {return"I was running."; }}vim demo1.phpfunction__autoload ($_classname){    require $_classname.". Class.php ";}$computer 1=Newcomputer ();Echo $computer 1-_name;Echo $computer 1->_run ();

__call () Mask Error method

//PHP uses the __call () built-in method to mask errors that occur when an object calls a method, and the __call () method is called automatically when an object invokes a nonexistent method. Tips:$methodName parameter is a string,$arguments is an array. Private methods, only allow calls inside the class
Although the log will appear warning message, but for the security of the program can be ignored !
classcomputer{ Public $_name= "Lenovo"; Public function__construct () {Echo"<meta charset=utf8>"; } //$_methodname is a string//$arguments is a parameter array Public function__call ($_methodname,$arguments) { Echo $_methodname." Method Error, the method you called does not exist, see if the current class exists with this method <br> "; Print_r($arguments); }}$computer 1=Newcomputer ();Echo $computer 1-_name;//call a non-existent method and write the parameter$computer 1->_run ("Zhang San", "open", "Notebook", "Open win7 System");

__tostring () to print a reference to an object

//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. tip: For security, you should set the private method to only allow calls within the classclasscomputer{ Public $_name= "Lenovo";  Public function__construct () {Echo"<meta charset=utf8>"; }    Private function__tostring () {return"I am the result of the __tostring method run"; }}$computer 1=Newcomputer ();//You can enter a reference to an objectEcho $computer 1;//you can also output when the class is instantiatedEcho NewComputer ();

__clone () Cloning method

First of all, compare the next three modes

# instantiate an object individually $computer 1 New computer (); $computer 2 New Computer ();

# instantiate an object, assign a reference to another variable $computer 1 New comoputer (); $computer 2 $computer 1;

# Clone mode $computer 1 New computer (); $computer 2 Clone $computer 1;

//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. classcomputer{ Public $_name= "Lenovo";  Public function__construct () {Echo"<meta charset=utf8>"; }     Public function_run () {Echo $this-_name; }     Public function__clone () {$this->_name= "IBM"; }}functionDiffcomputer () {$computer 1=Newcomputer (); $computer 1->_name = "DELL"; $computer 2=Newcomputer (); $computer 1-_run (); $computer 2-_run ();}functionTranscomputer () {$computer 1=Newcomputer (); $computer 2=$computer 1; $computer 1->_name= "DELL"; $computer 2-_run ();}functionClonecomputer () {$computer 1=Newcomputer (); //At this point only $computer 1 is cloned $computer 2//clone at the current moment will be $computer 2 point to the heap in memory _name field modified to "IBM" clone function is defined/that can be understood as the clone of the heap memory has been modified in the genus Sex    $computer 2=Clone $computer 1; $computer 2-_run (); $computer 1-_run ();}Echo"Diffcomputer function====>";d iffcomputer ();Echo"<br>";Echo"Transcomputer function=====>"; Transcomputer ();Echo"<br>";Echo"Clonecompuer function=====>"; Clonecomputer ();

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  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  get_class () function gets the class name of the object and returns False if it is not an object. Echo Get_class ($computer);

// The  get_class_methods () function Gets the method (public) in the class and returns it as an array.  print_r(get_class_methods($computer));

// The  get_class_vars () function gets the field (public) in the class and returns it as an array Print_r (get_class_vars(' computer '));

// the get_parent_class () function gets the parent class of the subclass if it does not return false; Echo Get_parent_class ("Notecomputer");

// the interface_exists () function determines whether the interface exists and returns true if present, otherwise false Echo interface_exists (' computer ');

// returns True if the is_a () function determines whether the object is a class or a parent class of this class, otherwise returns false Echo is_a ($computer. " Computer ");

// The  is_subclass_of () function determines whether the object is a subclass of the class, returns True, or returns false Echo is_subclass_of ($NoteComputer, "Computer");

// The  method_exists () function determines whether an object's method exists, returns True, or returns false Echo method_exists ($computer, ' _run ');

The Reflection API for OOP

// PHP5 classes and objects 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 members and protected members, but also know the parameters that its methods expect, and for this, use the reflection API. Learn the Reflection API can read PHP manual reading reflection This class

// get dump information for the reflection API $RC New Reflectionclass ("Computer"); Reflection:: Export ($RC);

// get information on PHP's built-in class library Reflection::export (new reflectionclass (' Reflection '));

// get an element in a class $_RC New Reflectionclass (' Comoputer '); Echo $_rc, GetFileName (); Echo $_RC->getname ();

PHP Object-oriented tools

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.