PHP object-oriented programming: method for developing large PHP projects (III) Author: LuisArgerich translator: limodou overload (different from overwriting) is not supported in PHP. In OOP, you can overload a method to implement two or more methods with the same name, but there are different numbers or types of parameters (SyntaxHighlighter. all
PHP object-oriented programming: method for developing large PHP projects (III) Author: Luis Argerich translator: limodou overload (different from overwriting) is not supported in PHP. In OOP, you can overload a method to implement two or more methods with the same name, but there are different numbers or types of parameters (depending on the language ). PHP is a loose language, so the type overload does not work, but the overload does not work by the number of parameters. Sometimes the overload constructor in OOP is very good, so that you can create objects using different methods (passing different numbers of parameters ). Tips for implementing it in PHP :-------------------------------------------------------------------------------- $ Name (); // note that $ this-> name () is generally incorrect, but here $ name is the name of the method to be called} function Myclass1 ($ x) {code;} function Myclass2 ($ x, $ y) {code ;}}?> ------------------------------------------------------------------------------ This class is transparent to users through additional processing in the class: $ obj1 = new Myclass (1 ); // call Myclass1 $ obj2 = new Myclass (1, 2); // call Myclass2, which is sometimes very useful. Polymorphism is a kind of object capability. it can decide which object method to call at runtime based on the passed object parameters. For example, if you have a figure class, it defines a draw method. The circle and rectangle classes are derived. in the derived class, you override the draw method. you may also have a function that wants to use a parameter x, you can call $ x-> draw (). If you have polymorphism, the draw method that you call depends on the object type that you pass to this function. Polymorphism is in an interpreted language like PHP (imagine which method should you call when a C ++ compiler generates such code? You don't know what type of objects you have. well, this is not the point.) it is very easy and natural. Therefore, PHP certainly supports polymorphism. -------------------------------------------------------------------------------- Draw () ;}$ obj = new Circle (3,187); $ obj2 = new Rectangle (); $ board-> niceDrawing ($ obj ); // call the draw method of Circle $ board-> niceDrawing ($ obj2); // call the draw method of Rectangle?> ------------------------------------------------------------------------------ Using PHP for object-oriented programming some "purists" may say that PHP is not a real object-oriented language. PHP is a hybrid language. you can use OOP or traditional procedural programming. However, for large projects, you may want/need to use pure OOP to declare classes in PHP, and only use objects and classes in your project. As the project grows, it may be helpful to use OOP, and the OOP code is easy to maintain, easy to understand and reuse. These are the basis of software engineering. Applying these concepts in web-based projects will become the key to future website success.