Introduction: This is a detailed page of PHP object-oriented basics. It introduces PHP, related knowledge, skills, experience, and some PHP source code.
Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 330423 'rolling = 'no'>
Problems Existing in traditional development methods:
1) Poor software reusability; 2) Poor Software maintainability; 3) poor scalability
Three main features of object-oriented: encapsulation, inheritance, and Polymorphism
1. Get started with object-oriented
Class, attribute, method, constructor, and destructor
<? Phpclass mypc {public $ name = "default"; var $ price; function mypc ($ name = "") {// if the default value is not assigned, if you do not pass a parameter when calling this method, a warning will be prompted: $ this-> name = $ name;} // function _ construct () {}// constructor function VOD () {return $ this-> name. "computer";} function _ destruct () {// destructor echo $ this-> name. "<br>"; // principle of backward-in-first-out} function test () {return "erased <br>" ;}} echo mypc: Test (); // directly access the method using the class name. There cannot be instance variable references in the method $ pC1 = new mypc ("my"); $ PC2 = new mypc ("your "); echo $ pC1-> VOD (). "<B R> "; $ pC1 = NULL; // manually recycle, automatically call the _ destruct () method echo $ PC2-> VOD (). "<br>"; $ PC2 = NULL; // manually recycle, automatically call the _ destruct () method?>
2. Class Encapsulation
Encapsulation is to hide some related attributes and behaviors to ensure protection and security.
Encapsulate keywords
Public indicates global and can be accessed by external subclass inside the class;
Protected indicates that it is protected and can be accessed only in this class or subclass or parent class;
Private indicates private, which can only be used inside the class;
<? PHP // class encapsulation and application class mypc {private $ name; private $ age; function _ construct ($ name = "") {return $ this-> name = $ name;} function _ Get ($ name) {return $ this-> name;} function _ set ($ N, $ V) {if ($ v> 100) {$ this-> $ n = $ V ;}} private function power () {return $ this-> name. "Power on";} function OK () {return $ this-> power (). ", boot successful" ;}}$ pC1 = new mypc ("computer"); $ pC1-> name = 1000; // If NO _ set () method is available, $ pC1-> age = 200; echo $ pC1-> name; // If the _ Get () method is not available, error e is reported. Cho $ pC1-> age; // _ Get () and _ set () methods/* problems: the _ Get method and _ set method can only be defined once in the class. If there are multiple private attributes, how to specify the corresponding get and Set Methods _ Get and _ set are only used to obtain and set attribute values. If there are multiple, just set one by one in the method body */?>
3. class inheritance, abstraction, interface, and Polymorphism
<? PHP // use extends in PHP for single inheritance class root {function root1 () {return "root print <br>" ;}} class son extends root {function root1 () {// override the method of the parent class. If you want to access the method of the parent class, you can use the Class Name: Method Name () to access return "root :". root: root1 (). "In son's root";} function son1 () {// at this time, the $ this variable can be used to access the method return $ this-> root1 () of the parent class (). "Son print <br>" ;}}$ P = new son (); echo $ p-> son1 (); echo $ p-> root1 (); // abstract class // PHP abstract classes must contain abstract methods. When inheriting abstract classes, abstract methods must be implemented. // in abstract classes of Java, abstract class A { BSTRACT function fun1 (); abstract function fun2 (); function OK () {}} Class B extends a {function fun1 () {echo "func1";} function fun2 () {echo "func2" ;}}// interface/** required keyword interface implements * usage: The same as Java * If You Want To Inherit and implement interfaces at the same time, first inherit the interfaces, single inheritance multiple interfaces ** // polymorphism/* OPERATOR: instanceof ***/Class C {} Class D {}$ A = new C; // when new C is used, you do not need to () // echo $ A; if ($ A instanceof c) {echo '<br> C' ;}?>
4. Some methods
_ Tostring (), _ call (), _ clone (), and _ autoload ()
<? PHP // _ tostring () method, similar to the tostring method of Java // Exception Handling of object Methods _ call ($ funname, $ arr_value)/** if the called method does not exist, the _ call Method * _ call method is automatically called to debug and absorb errors. ** // The application that cloned the object/** method name__ clone () * keyword clone * // Method for automatically loading objects/** method name_autoload () * This method is a separate method except the class **/class my {public $ name = "My Class"; function _ tostring () {return $ this-> name. "tostring <br>";} function _ call ($ methodname, $ value) {echo "the method that does not exist is :". $ methodname. "<br>"; echo "Error Code :". print_r ($ value ). "<br>"; // The parameter value is transmitted as an array. Therefore, use print_r to print} function _ destruct () {echo "to clear an object <br> ";} function _ clone () {// This method is automatically called when cloning with the clone keyword $ this-> name = "cloned value ";}} $ P = new my (); echo $ P; $ p-> demo ("test"); // If the demo method does not exist, the _ call method is called, if no _ call method exists, an error will be returned: $ B = clone $ P; // After cloning an object, __destruct () will be executed twice echo $ B-> name. "<br>"; function _ autoload ($ class_name) {include ($ class_name. ". PHP ");} $ Al = new autoload ( ); $ Al-> autoloadtest ();?>
5. Common keywords
<? PHP // common keywords in the class/** final ** is an important keyword used to define classes and methods. * when defining classes, this class cannot be inherited, when defining a method, this method cannot be overloaded. ** self ** is used to use a class to access the content keywords in the current class. * similar to the $ this keyword, however, $ this can be used only after the class is instantiated. Self can directly access internal members of the current class. ** it makes no sense to access internal attributes or methods without instantiating the class, therefore, self is generally used for static members, constants, or other definition content * Self: internal class members (attributes or methods) <==> Class Name :: class internal members (attributes or methods) * problem: difference between self and this ** static * is used to define static attributes or methods of the class, when the class is not instantiated, you can use * Static attributes to occupy memory independently, instead of occupying the same method or attribute repeatedly when multiple objects are created. ** Non-static content is not allowed in the static method (this keyword cannot be used) ** access to static members: * Class Name: static member * Self :: the static member ** const ** is used to define constants in the class, similar to the keyword define () for the PHP external definition constant. * const can only modify the member attributes in the class! * 1. It is recommended to use uppercase constants * 2. Do not use the $ symbol * constant access: * Class Name: constant * Self: constant **/?>
More articles on "php object-oriented basics"
Love J2EE follow Java Michael Jackson video station JSON online tools
Http://biancheng.dnbcw.info/php/330423.html pageno: 11.