In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what is the meaning of overloading in PHP". In daily operation, I believe that many people have doubts about the meaning of overloading in PHP. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "what is the meaning of overloading in PHP?" Next, please follow the editor to study!
What is the "overload" in PHP?
Many interviewers will ask some object-oriented questions during the interview. among the three characteristics of object-oriented, the most important way to realize polymorphism is method reloading and rewriting. But in PHP, there is only rewriting, and there is no full implementation of overloading capabilities.
Overrides, and the subclass overrides the parent method.
/ / rewrite
Class A
{
Public function test ($a)
{
Echo 'This is aura'. $a, PHP_EOL
}
}
Class childA extends A
{
Public function test ($a)
{
Echo 'This is A child:'. $a, PHP_EOL
}
}
$ca = new childA ()
$ca- > test (1)
There is no problem with this in PHP, and subclasses can override the methods of the parent class. When a subclass is instantiated, the overridden method implemented by the subclass is called.
Overloaded, same method name but different number or type of parameters.
Class A {
Function foo ($a) {
Echo $a
}
/ / Fatal error: Cannot redeclare A::foo ()
Function foo ($a, $b) {
Echo $astatb
}
}
Sorry, the result of this will be a direct error report. PHP does not support such overloading capabilities. In PHP's official manual, overloading is defined as using magic methods such as _ _ set (), _ _ get (), _ _ call (), _ _ callStatic () to overload inaccessible variables or methods. This is completely different from the object-oriented overloading we learned, and there are a lot of questions about it in the note in the manual. Of course, we are not going to talk about the use of these magic methods today. About their use, you can refer to the articles we have written before: those magic methods in PHP (1) and those magic methods in PHP (2)
So, can overloading be implemented in PHP? Of course, but it will be a little more troublesome:
/ / overload
Class B
{
Public function foo (... $args)
{
If (count ($args) = = 2) {
$this- > fooAdd (... $args)
} else if (count ($args) = = 1) {
Echo $args [0], PHP_EOL
} else {
Echo 'other'
}
}
Private function fooAdd ($a, $b)
{
Echo $a + $b, PHP_EOL
}
}
B = new B ()
$b-> foo (1)
$b-> foo (1,2)
Using one method to call other methods, judging according to the number of parameters, you can achieve method overloading with different number of parameters.
/ / use _ _ call () for overloading
Class C
{
Public function _ _ call ($name, $args)
{
If ($name = = 'foo') {
$funcIndex = count ($args)
If (method_exists ($this, 'foo'. $funcIndex)) {
Return $this- > {'foo'. $funcIndex} (... $args)
}
}
}
Private function foo1 ($a)
{
Echo $a, PHP_EOL
}
Private function foo2 ($a, $b)
{
Echo $a + $b, PHP_EOL
}
Private function foo3 ($a, $b, $c)
{
Echo $a + $b + $c, PHP_EOL
}
}
C = new C ()
$c-> foo (1)
$c-> foo (1,2)
C-> foo (1, 2, 3)
It may be easier to use the _ _ call () magic method, but it will also give some beginners a circle when taking over the project. After all, the magic method is unfriendly to IDE, and such development makes _ _ call () a template method that defines the algorithmic skeleton of the operation. We can also simulate the overload capability according to the parameter type.
/ / overloads with different parameter types
Class D {
Function _ _ call ($name, $args) {
If ($name = = 'foo') {
If (is_string ($args [0])) {
$this- > fooString ($args [0])
} else {
$this- > fooInt ($args [0])
}
}
}
Private function fooInt (int $a) {
Echo $a. ' Is Int', PHP_EOL
}
Private function fooString (string $a) {
Echo $a. ' Is String', PHP_EOL
}
}
$d = new D ()
$d-> foo (1)
$d-> foo ('1')
In any case, the method overloading implemented with the above method is very troublesome because it makes a method or magic method very heavy, and it needs to become a controller to schedule the internal method according to the parameters. More often, we should use different method names and abstract public parts into independent private internal methods to achieve "overloading" of different method names. After all, different languages still need to master their different personalities, and flexibly apply them to our projects according to these personalities.
At this point, the study of "what is the meaning of overloading in PHP" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
# # queue##!/usr/bin/env pytho
© 2024 shulou.com SLNews company. All rights reserved.