首頁  >  文章  >  後端開發  >  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
上一篇:在 PHP 中重寫下一篇:在 PHP 中重寫