Can I / How ... call a protected function outside a class in PHP?

I have a protected function that is defined in a specific class. I want to be able to call this protected function outside the class in another function. Is it possible, and if so, how can I achieve it.

class cExample{ protected function funExample(){ //functional code goes here return $someVar }//end of function }//end of class function outsideFunction(){ //Calls funExample(); } 
+12
source share
6 answers

That the OOP point is encapsulation:

Private

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

Protection

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

Public

 Can be used anywhere. Inherited by child classes. 

If you still want to call this function from the outside, you can declare a public method that runs your protected method:

 protected function b(){ } public function a(){ $this->b() ; //etc } 
+11
source

Technically, you can use private and protected methods using the reflection API. However, 99% of the time is a really bad idea. If you can change the class, then the right solution will probably just make this method publicly available. In the end, if you need to access it outside the class, this will cause its mark to be protected.

Here is an example of a quick reflection, if this is one of the very few situations when it is really necessary:

 <?php class foo { protected function bar($param){ echo $param; } } $r = new ReflectionMethod('foo', 'bar'); $r->setAccessible(true); $r->invoke(new foo(), "Hello World"); 
+36
source

You can override this class with others where you publish it.

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

(note that this will not work with private members)

But the idea of ​​private and protected members should NOT be triggered from outside.

+3
source

If you want to use code between your classes, you can use traits, but it depends on how you want to use your function / method.

Anyway

 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(); 

This will work, but keep in mind that you do not know what your class does in this way. If you change the trait, all of your classes that use it will also be changed. It is also good practice to write an interface for each attribute that you use.

+2
source

here I can give one example, for example below

 <?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(); ?> 

or one way to use with the latest php

In PHP, you can do this with Reflections. To call protected or private methods, use the setAccessible () method of http://php.net/reflectionmethod.setaccessible (just set it to TRUE)

+1
source

If you know what you are doing, you can use an anonymous class (PHP 7+)

 class Bar { protected baz() { return 'Baz!'; } } $foo = new class extends Bar { public baz() { parent::baz(); } } $foo->baz(); // "Baz!" 

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

0
source

Source: https://habr.com/ru/post/947573/


All Articles