Home > Backend Development > PHP Tutorial > Method Three for Developing Large PHP Projects_PHP Tutorial

Method Three for Developing Large PHP Projects_PHP Tutorial

WBOY
Release: 2016-07-13 17:25:38
Original
829 people have browsed it

Overloading (unlike overriding) is not supported in PHP. In OOP, you can overload a method to implement two or more methods with the same name but different numbers or types of parameters (depending on the language). PHP is a loosely typed language, so overloading by type will not work, but overloading by different number of parameters will not work either. Sometimes it's good to overload constructors in OOP so that you can create objects in different ways (passing different numbers of arguments). The trick to do it in PHP is: ----------------------------------------- ------------ $name(); //Note that $this->name() is generally wrong, but here $name is the name of the method that will be called} function Myclass1($x) { code; } function Myclass2($x,$y) { code; } } ?>------------------------- -------------------------- Using this class is transparent to the user through additional processing in the class: $obj1=new Myclass( 1); //Myclass1 will be called $obj2=new Myclass(1,2); //Myclass2 will be called Sometimes this is very useful. Polymorphism Polymorphism is an ability of an object, which can determine which object method to call based on the passed object parameters at run time. For example, if you have a figure class, it defines a draw method. And derived the circle and rectangle classes, in the derived class you override the draw method, you may also have a function, which expects a parameter x, and can call $x->draw(). If you have polymorphism, which draw method is called depends on the type of object you pass to the function. Polymorphism is very important in interpreted languages ​​like PHP (imagine a C++ compiler generating code like this, which method should you call? You also don't know what type of object you have, well, that's not the point) Easy and natural. So of course PHP supports polymorphism. -------------------------------------------------- --- draw(); } $obj=new Circle(3,187); $obj2=new Rectangle(4,5); $board->niceDrawing($obj); //Circle’s draw method will be called$ board->niceDrawing($obj2); //The draw method of Rectangle will be called?>---------------------------------- -------------------- Object-oriented programming with PHP Some "purists" may say that PHP is not a true object-oriented language. fact. PHP is a hybrid language. You can use OOP or traditional procedural programming. However, for larger projects, you may want/need to use pure OOP to declare classes in PHP, and only use objects and classes in your project. As projects get larger, it may be helpful to use OOP. OOP code is easy to maintain, easy to understand and reuse. These are the foundations of software engineering. Applying these concepts in web-based projects becomes the key to future website success.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/532031.htmlTechArticleOverloading (unlike overriding) is not supported in PHP. In OOP, you can overload a method to implement two or more methods with the same name but different numbers or types of parameters...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template