PHP Object-oriented features and Java ideas are the same, but a weak type of language is a strong type of language, the implementation of the two or there are many different points, the function can only point to stop, because I often confused, so write this article to summarize the PHP object-oriented knowledge points, Convenient for future queries; class Person{var $name;p rivate $age; function person () {//Old constructor method, method name and class name same//constructor is automatically executed when object is generated, usually used to initialize variable} function __construct () {//php5 New construction method}function __destruct () {//destructors///objects are automatically executed after a reference is lost (the reference is placed on the stack, the object is placed in the heap)}private function Study () {//keyword private is used to declare a private property or method, a private property or method cannot be accessed externally,//can only be accessed within a class, and private properties and methods cannot be inherited}protected function study () {// The protected keyword is used to declare a protected property or method that a protected property or method cannot be accessed externally, or that can be accessed only within a class, but protected properties or methods may be inherited}public function study () {// Public declares a common method or property, omitting a function or method that is not written is the default public} Some of the common system Methods __set (), __get (), __isset (), __unset (), __tostring (), __clone (), __ Call (), __autoload (), function __get ($name) {//is called automatically when an external attempt is made to access the private property of an object, and the accepted parameter is an externally attempted property name}function __set ($name, $value ) {//When external attempts to set the private property of an object are called automatically, and the parameters that are accepted are the property name and value}function __isset ($name) {////////When external use of the isset () function to test the existence of a private property of an object, the function is executed. Auto-Accept Test property name}function __unset ($name) {//when externally using the unset () function to attempt to delete a private property of an object when automatically called, automatically accept the name of the property you are trying to delete}function __tostring () {// If the direct outputA reference (pointer) to an object will automatically call this function return str; You must return a string}function __clone () {///If you clone an object externally using the Clone keyword ($a 1=clone $a 2), the function is automatically called//can be used to initialize, at which time the $this represents the newly cloned object $a1, Representing the cloned object is $that//$a 1=clone $a 2 is different from $a1= $a 2, the former creates an object in the heap, the latter is just the same object with one pointer pointing to it}function __call ($funName, $argus) {// This function is automatically called when a method that does not exist for an object is called, automatically accepting the parameter $funname (nonexistent function name)//parameter $argus array}__autoload () function: This function is not written in the object, is written at the top of a PHP file to automatically load this PHP page using the class function __autoload ($className) {include $className. " PHP "//assemble into a full path}} Inheritance: Class boy extends Person{var $hoby; function overloading is not supported in PlayGame () {}}php, because of the reason for weak type, the type of the parameter is indeterminate, Because PHP supports function dynamic parameters, so the number of arguments is not deterministic, so PHP can only override the function (method override) method overrides: In the subclass method add a sentence Paret:: Method Name (); The code inside the parent's method is added to the new method. Common keywords in PHP object-oriented: final Static constfinal class person{//plus keyword final classes cannot be inherited by the final function study () {//functions using the final keyword cannot be overloaded (overridden)}} A method or property that uses the static adornment can use the class name directly without instantiating the object:: Method () or class Name:: Property static method cannot use non-static property const-decorated constant: const num=100; constant name is used to capitalize keyword $this and self former represents this object, The latter represents serialization and crossdress serialization of this class of objects: The Serialize (value) parameter is the object name, and the return value of the function is a string crossdress: the Unserialize (str) parameter is a string, and the return value of the function is when the object is used: 1, when the network is transmitting, 2, part serialization when writing an object to a file or database: Add this party to the objectMethod: Function __sleep () {$arr = array ("name", "Age"),//The property to be serialized is passed in as an array of return $arr;} This method is called automatically when crossdress is serialized (if the object is not written): function __wakeup () {///similar to __clone (), mainly doing some initialization work} abstract methods and abstract classes: abstract function abd (); Only the method name, no method body contains abstract methods of the class is called abstract class, also need to use the abstract keyword to modify the abstraction class is on the basis of ordinary class more abstract method abstract class can not be instantiated, only as a canonical inheritance abstract class must implement the abstract method inside, otherwise it is abstract class, Cannot instantiate object interface: interface declaration interface demo{} All member properties must be constants all methods are abstract all members must be public one class can inherit only one parent class, but can implement multiple interfaces class one implements two{} Polymorphism in PHP (unlike Java) is usually implemented in PHP: The parent class is usually an abstract class or an interface is implemented by subclasses, and the subclass contains the same method name, but the implementation of each subclass is different
http://www.bkjia.com/PHPjc/735892.html www.bkjia.com true http://www.bkjia.com/PHPjc/735892.html techarticle PHP Object-oriented features and Java ideas are the same, but a weak type language is a strong type of language, the implementation of the two are many different points, the function can only point to ...