首页  >  文章  >  后端开发  >  PHP 中的方法重载

PHP 中的方法重载

PHPz
PHPz原创
2024-08-29 12:59:19721浏览

方法重载是属性重载之外的一种重载。它是创建一个/多个不在该类范围内创建的动态方法。 PHP 方法重载概念还有助于触发为适当目的指定的神奇方法。除了属性重载概念之外,PHP 方法的重载概念还允许在对象和静态上下文上进行函数调用。基本上是OOP的方法之一。

广告 该类别中的热门课程 PHP 开发人员 - 专业化 | 8 门课程系列 | 3次模拟测试

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

语法:

Public _call (string $name1 , array $arguments1 ) : mixed
Public static _callStatic (string $name1 , array $arguments1 ) : mixed

方法重载在 PHP 中如何工作?

方法重载通过创建动态方法来处理类内部的声明。它还通过出于适当的目的触发一些魔术方法来工作,并在静态上下文和对象上调用函数/函数调用。方法重载的概念也适用于大多数其他编程语言,如 c、java 等。我们将方法重载称为静态多态性。

有一些神奇的功能,它们是:

  • _call():对象的上下文可以使用call()函数触发重载方法的执行。
  • _callStatic(): callstatic() 魔术函数激活静态上下文中的重载概念/方法。

PHP 中方法重载的示例

以下是下面提到的 PHP 中方法重载的示例

示例#1

在下面的 PHP 编程语言中,$name1 参数是要调用的方法的名称,而 $arguments 是包含用于传递给 $name'ed 方法的参数/参数的枚举数组之一。

_call() 函数使用 2 个参数 $name1 和 $arguments1。 Implode() 函数从数组元素(即字符串/句子)返回一个字符串。在 Implode(separator, array) 中,分隔符是可选参数,但只是建议使用这两个参数以实现向后兼容性。分隔符参数中特定类型的分隔符将向数组参数中存在的单词/字符串插入分隔符。

Obj 变量将创建一个名为 SPK 的新对象。对象->将有助于访问对象的方法和属性。 Spk 将从静态上下文中执行,而 obj 将从对象上下文中运行。

代码:

<?php
class SPK {
public function __call($name1, $arguments1) {
echo "object method calling '$name1' "
. implode(', ', $arguments1). "\n";
}
public static function __callStatic($name1, $arguments1) {
echo "static method Calling '$name1' "
. implode(', ', $arguments1). "\n";
}
}
// Create new object
$obj = new SPK;
$obj->runTest('in one of the object context');
SPK::runTest('in one of the static context');
?>

输出:

PHP 中的方法重载

示例#2

代码示例使用单个 _call() 函数定义 foo1 类,该函数执行 die() 函数以显示消息并终止当前 PHP 脚本。 Die() 与 exit() 函数相同,后者只接受括号内的一个参数。

Foo1->将有助于从变量 $foo1.

访问对象的方法和属性

代码:

<?php
class Foo1 {
function __call($m1, $a1) {
die($m1);
}
}
$foo1 = new Foo1;
print $foo1->{' wow !'}();
// outputs ' wow !'
?>

输出:

PHP 中的方法重载

示例 #3

这是 PHP 编程语言中使用 call() 函数和私有/受保护方法重载方法的示例。

这里调用私有/受保护的方法是通过拼写错误或其他原因访问来完成的。

Echo _METHOD_PHP_EOL 将返回使用的方法类型。它仅使用 _call() 函数,该函数从对象上下文运行。

代码:

<?php
class TestMagicCallMethod1 {
public function foo1()
{
echo __METHOD__.PHP_EOL;
}
public function __call($method1, $args1)
{
echo __METHOD__.PHP_EOL;
if(method_exists($this, $method1))
{
$this->$method1();
}
}
protected function bar1()
{
echo __METHOD__.PHP_EOL;
}
private function baz1()
{
echo __METHOD__.PHP_EOL;
}
}
$test    =    new TestMagicCallMethod1();
$test->foo1();
$test->bar1();
$test->baz1();
?>

输出:

PHP 中的方法重载

示例#4

这是call()和call static()函数概念的程序,用于方法重载概念。下面的这个 PHP 程序将首先调用实例中的 _call() 函数,然后再调用 _callstatic() 函数。

Var dump() 将提供有关 PHP 和其他一些面向对象编程语言中括号中的变量的信息。除此之外,一切都是一样的,就像上面的例子一样。

代码:

<?php
class A1 {
public function test1 () {
static::who();
A1::who();
self::who();
$this->who();
}
public static function __callStatic($a1, $b1) {
var_dump('A1 static');
}
public function __call($a1, $b1) {
var_dump('A1 call');
}
}
$a1 = new A1;
$a1->test1();
?>

输出:

PHP 中的方法重载

示例#5

这是 _call() 函数的示例;如果使用甚至不存在的方法调用对象的类,则调用 _call() 函数的概念而不是方法。

Execute the _call() function to support the concept of method overloading through the dynamic area() method included in the PHP program below. The object’s behavior will vary depending on the parameters that pass to it.

Code:

<?php
class Shape1 {
const PI1 = 3.142 ;
function __call($name1,$arg1){
if($name1 == 'area1')
switch(count($arg1)){
case 0 : return 0 ;
case 1 : return self::PI1 * $arg1[0] ;
case 2 : return $arg1[0] * $arg1[1];
}
}
}
$circle1 = new Shape1();
echo $circle1->area1(3);
$rect1 = new Shape1();
echo $rect1->area1(8,6);
?>

Output:

PHP 中的方法重载

Example #6

Here, the _call() and _callstatic() functions are used like in the 1st example.

Code:

<?php
class Toys1
{
public function __call($name1,$pavan1){
echo "Magic method invoked while method overloading with object reference";
}
public static function __callStatic($name1,$pavan1){
echo "Magic method invoked while method overloading with static access";
}
}
$objToys1 = new Toys1;
$objToys1->overloaded_method();
Toys1::overloaded_property();
?>

Output:

PHP 中的方法重载

Example #7

The call () function of method Overloading triggered and invoked the inaccessible methods in the object context. Call() is mixed with the syntax _call(string $name1 , array $arguments1).

Then $name1 parameter is for the name of the method which is to be called, whereas the array $arguments1 is the parameter that is an enumerated array that contains/has the parameters which are to be passed to the $name variables method.

Code:

<?php
class ABC1 {
public function __call($method_name1, $arguments1) {
$methodArray1 = array('displayMessage11','displayMessage12');
if (in_array($method_name1,$methodArray1) === false) {
die("\n Method does not exist");
}
if (count($arguments1) === 2) {
$this->displayMessage12($arguments1[0],$arguments1[1]);
}
elseif (count($arguments1) === 1) {
$this->displayMessage11($arguments1[0]);
} else {
echo "\n unknown method";
return false;
}
}
function displayMessage11($var11) {
echo "\n from func1($var11)";
}
function displayMessage12($var11,$var12) {
echo "\n from func2($var11,$var12)";
}
}
$obj1 = new ABC1;
$obj1->displayMessage11('hello');
$obj1->displayMessage12('hello','hello2');
$obj1->displayMessage13('Hello');
?>

Output:

PHP 中的方法重载

Example #8

It is also just like the first example program. Check it once.

Code:

<?php
class MethodOverloading1
{
public function __call($name1,$pavan1){
echo "\n--It is now With the object reference ";
}
public static function __callStatic($name1,$pavan1){
echo "\n-----It is now With the static reference \n";
}
}
// Here now creating the object of the class " MethodOverloading "
$obj1 = new MethodOverloading1;
echo "Method Overloading1 Now in Command ";
// Now using the object's reference
$obj1->DemoTest1();
// Now using the static's reference
MethodOverloading1::DemoTest1();
?>

Output:

PHP 中的方法重载

Example #9

This program shows the area of the circle and rectangle using some parameters and the call() function of the method overloading concept. The program will only run with the object context due to the object assigning an object variable to the class, etc.

Code:

<?php
class TDshape1 {
const Pi1 = 3.142 ;  // constant value
function __call($fname1, $argument1){
if($fname1 == 'area1')
switch(count($argument1)){
case 0 : return 0 ;
case 1 : return self::Pi1 * $argument1[0] ; // 3.14 * 15
case 2 : return $argument1[0] * $argument1[1];  // 5 * 11
}
}
}
$circle1 = new TDshape1();
echo "Area of the circle:".$circle1->area1(15); // display output of the area of circle
$rect1 = new TDshape1();
echo "\n Area of the rectangle:".$rect1->area1(5,11); // display output of the area of rectangle
?>

Output:

PHP 中的方法重载

以上是PHP 中的方法重载的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:Overriding in PHP下一篇:Inheritance in PHP