簡體   English   中英

我可以/如何...在 PHP 中的類之外調用受保護的函數

[英]Can I/How to... call a protected function outside of a class in PHP

我有一個在某個類中定義的受保護的函數。 我希望能夠在另一個函數的類之外調用這個受保護的函數。 這可能嗎,如果可以,我該如何實現

class cExample{

   protected function funExample(){
   //functional code goes here

   return $someVar
   }//end of function

}//end of class


function outsideFunction(){

//Calls funExample();

}

從技術上講,可以使用反射 API 調用私有和受保護的方法。 然而,在 99% 的情況下這樣做是一個非常糟糕的主意。 如果您可以修改類,那么正確的解決方案可能是將方法設為公開。 畢竟,如果您需要在類之外訪問它,那就失去了將其標記為受保護的意義。

這是一個快速反射示例,以防這是真正必要的極少數情況之一:

<?php
class foo { 
    protected function bar($param){
        echo $param;
    }
}

$r = new ReflectionMethod('foo', 'bar');
$r->setAccessible(true);
$r->invoke(new foo(), "Hello World");

這就是 OOP 的重點——封裝:

私人

Only can be used inside the class. Not inherited by child classes.

受保護

Only can be used inside the class and child classes. Inherited by child classes.

公眾號

Can be used anywhere. Inherited by child classes.

如果您仍然想在外部觸發該函數,您可以聲明一個觸發受保護方法的公共方法:

protected function b(){

}

public function a(){
  $this->b() ;
  //etc
}

你可以用另一個你公開的類來覆蓋這個類。

class cExample2 extends cExample {
  public function funExample(){
    return parent::funExample()
  }
}

(請注意,這不適用於私人成員)

但是私有成員和受保護成員的想法是不能從外部調用的。

如果你想在你的類之間共享代碼,你可以使用特征,但這取決於你想如何使用你的函數/方法。

反正

trait cTrait{
   public function myFunction() {
      $this->funExample();
   }
}

class cExample{
   use cTrait;

   protected function funExample() {
   //functional code goes here

   return $someVar
   }//end of function

}//end of class

$object = new cExample();
$object->myFunction();

這會起作用,但請記住,您不知道您的類是由這種方式構成的。 如果您更改特性,那么所有使用它的類也將被更改。 為您使用的每個特征編寫一個接口也是一種很好的做法。

在這里我可以給你一個像下面這樣的例子

<?php
    class dog {
        public $Name;
        private function getName() {
            return $this->Name;
        }
    }

    class poodle extends dog {
        public function bark() {
            print "'Woof', says " . $this->getName();
        }
    }

    $poppy = new poodle;
    $poppy->Name = "Poppy";
    $poppy->bark();
?>

或另一種與最新 php 一起使用的方法

在 PHP 中,您可以使用反射來做到這一點。 要調用受保護或私有方法,請使用 setAccessible() 方法http://php.net/reflectionmethod.setaccessible (只需將其設置為 TRUE)

如果父方法受保護,則可以使用匿名類:

class Foo {
    protected function do_foo() {
        return 'Foo!';
    }
}

$bar = new class extends Foo {
    public function do_foo() {
        parent::do_foo();
    }
}

$bar->do_foo(); // "Foo!"

https://www.php.net/manual/en/language.oop5.anonymous.php

另一種選擇(PHP 7.4)

<?php
class cExample {
   protected function funExample(){
       return 'it works!';
   }
}

$example = new cExample();

$result = Closure::bind(
    fn ($class) => $class->funExample(), null, get_class($example)
)($example);

echo $result; // it works!

我正在使用 Laravel。 我在課堂外訪問受保護的方法時遇到了問題。

   $bookingPriceDetails = new class extends BookingController {
        public function quotesPrice( $req , $selectedFranchise) {
           return parent::quotesPrice($req , $selectedFranchise);
        }
    };

     return $bookingPriceDetails->quotesPrice($request , selectedFranchisees());

這里BookingController是我想從中獲取protected方法的名。 quotesPrice( $req , $selectedFranchise)是我想在不同的類中訪問的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM