How do you call a function outside the class in php?
Ngày đăng:
19/09/2022
Trả lời:
0
Lượt xem:
52
Show
Can I/How to... call a protected function outside of a class in PHPclass cExample{ protected function funExample(){ //functional code goes here return $someVar }//end of function }//end of class function outsideFunction(){ //Calls funExample(); } setAccessible(true); $r->invoke(new foo(), "Hello World"); 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 Foo { protected function do_foo() { return 'Foo!'; } } $bar = new class extends Foo { public function do_foo() { return parent::do_foo(); } } $bar->do_foo(); // "Foo!" 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(); $class->funExample(), null, get_class($example) )($example); echo $result; // it works! Name; } } class poodle extends dog { public function bark() { print "'Woof', says " . $this->getName(); } } $poppy = new poodle; $poppy->Name = "Poppy"; $poppy->bark(); ?> $bookingPriceDetails = new class extends BookingController { public function quotesPrice( $req , $selectedFranchise) { return parent::quotesPrice($req , $selectedFranchise); } }; return $bookingPriceDetails->quotesPrice($request , selectedFranchisees()); Can I/How to... call a protected function outside of a class in PHPclass cExample{ protected function funExample(){ //functional code goes here return $someVar }//end of function }//end of class function outsideFunction(){ //Calls funExample(); } setAccessible(true); $r->invoke(new foo(), "Hello World"); 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 Foo { protected function do_foo() { return 'Foo!'; } } $bar = new class extends Foo { public function do_foo() { return parent::do_foo(); } } $bar->do_foo(); // "Foo!" 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(); $class->funExample(), null, get_class($example) )($example); echo $result; // it works! Name; } } class poodle extends dog { public function bark() { print "'Woof', says " . $this->getName(); } } $poppy = new poodle; $poppy->Name = "Poppy"; $poppy->bark(); ?> $bookingPriceDetails = new class extends BookingController { public function quotesPrice( $req , $selectedFranchise) { return parent::quotesPrice($req , $selectedFranchise); } }; return $bookingPriceDetails->quotesPrice($request , selectedFranchisees()); How to call a protected method in php?class Observer { protected callme() { } } class Parent extends Observer { function createChild() { $this->callme(); // this is OK return new Child ($this); } } class Child { private $this myParent; public function __constructor ($myParent) { $this->myParent = $myParent; } public function __destroy() { $this->myParent->callme(); // FAIL! } } class Child extends Parent { public function __constructor () { parent::__constructor(); } public function __destroy() { $this->callme(); // Should work! } } callme(); // this is OK return new MyChild ($this); } } class MyChild extends Observer // hackey extends { private $myMyParent; public function __construct($myMyParent) { $this->myMyParent = $myMyParent; $this->myMyParent->callme(); } } $p = new MyParent; $c = $p->createMyChild(); $ php hacky.php I was called from MyParent I was called from MyParent class Parent extends Observer { function createChild() { $this->callme(); // this is OK return new Child (function() { $this->callme(); }); } } class Child { private $gatewayFunction; public function __constructor (Closure $gatewayFunction) { $this->gatewayFunction = $gatewayFunction; } public function __destroy() { $this->gatewayFunction->__invoke(); } } Accessing a protected member variable outside a classclass Foo { protected $bar; } $foo = new Foo(); $rp = new ReflectionProperty('Foo', 'bar'); $rp->setAccessible(true); echo $rp->getValue($foo); class Foo { protected $bar = 'Hello World!'; public function getBar() { return $this->bar; } } $baz = new Foo(); echo $baz->getBar(); class MyClass { protected $variable = 'I am protected variable!'; } $closure = function() { return $this->variable; }; $result = Closure::bind($closure, new MyClass(), 'MyClass'); echo $result(); // I am protected variable! function getProtectedMember($class_object,$protected_member) { $array = (array)$class_object; //Object typecast into (associative) array $prefix = chr(0).’*’.chr(0); //Prefix which is prefixed to protected member return $array[$prefix.$protected_member]; } class ForExample { protected $var=122; } $call=function(){ echo $this->var; }; $call->call(new ForExample()); class MyClass : public OldClass { public: int getSomeValue() { return protectedValue; } void setSomeValue(int value) { protectedValue=value; } char* getOtherValue() { return otherProtectedValue; } } MyClass* blah = (MyClass*)TheirFactory->GiveMeAClass(); int yay=blah->getSomeValue(); Call private methods and private properties from outside a class in PHPclass Console { final public static function run() { while (TRUE != FALSE) { echo "\n> "; $command = trim(fgets(STDIN)); switch ($command) { case 'exit': case 'q': case 'quit': echo "OK+\n"; return; default: ob_start(); eval($command); $out = ob_get_contents(); ob_end_clean(); print("Command: $command"); print("Output:\n$out"); break; } } } } Class Demo { private $a; final public function myMethod() { // some code Console::run(); // some other code } final public function myPublicMethod() { return "I can run through eval()"; } private function myPrivateMethod() { return "I cannot run through eval()"; } } > $this->myPublicMethod(); > $this->myPrivateMethod(); class LockedGate { private function open() { return 'how did you get in here?!!'; } } $object = new LockedGate(); $reflector = new ReflectionObject($object); $method = $reflector->getMethod('open'); $method->setAccessible(true); echo $method->invoke($object); class Foo { private $bar = "Foo::Bar"; private function add_ab($a, $b) { return $a + $b; } } $foo = new Foo; // Single variable example $getFooBarCallback = function() { return $this->bar; }; $getFooBar = $getFooBarCallback->bindTo($foo, 'Foo'); echo $getFooBar(); // Prints Foo::Bar // Function call with parameters example $getFooAddABCallback = function() { // As of PHP 5.6 we can use $this->fn(...func_get_args()) instead of call_user_func_array return call_user_func_array(array($this, 'add_ab'), func_get_args()); }; $getFooAddAB = $getFooAddABCallback->bindTo($foo, 'Foo'); echo $getFooAddAB(33, 6); // Prints 39 $foo = new Foo; // Single variable example $getFooBar = function() { return $this->bar; }; echo $getFooBar->call($foo); // Prints Foo::Bar // Function call with parameters example $getFooAddAB = function() { return $this->add_ab(...func_get_args()); }; echo $getFooAddAB->call($foo, 33, 6); // Prints 39 $demo = new Demo(); $reflection_class = new ReflectionClass("Demo"); $reflection_method = $reflection_class->getMethod("myPrivateMethod"); $reflection_method->setAccessible(true); $result = $reflection_method->invoke($demo, NULL); public function callPrivateMethod($object, $methodName) { $reflectionClass = new \ReflectionClass($object); $reflectionMethod = $reflectionClass->getMethod($methodName); $reflectionMethod->setAccessible(true); $params = array_slice(func_get_args(), 2); //get all the parameters after $methodName return $reflectionMethod->invokeArgs($object, $params); } private function _myPrivateMethod() public function _myPrivateMethod() callprivate('somePrivateMethod'); request=$request; $this->data=$data; if($this->request=='email'){ $this->update_email(); } else{ echo "Can't do anything"; } } private function update_email(){ echo $this->request; echo '\n'; foreach($this->data as $x){ echo $x."\n"; } } } ?> How to access private/protected method outside a class in C++This is the private disp method of child class The key is 1019 This is the protected disp method of child class The key is 1019 PHP call Class method / function$var = filter($_GET['params']); class Functions{ public function filter($data){ $data = trim(htmlentities(strip_tags($data))); if(get_magic_quotes_gpc()) $data = stripslashes($data); $data = mysql_real_escape_string($data); return $data; } } $functions = new Functions(); $var = $functions->filter($_GET['params']); public static function filter($data){ $var = Functions::filter($_GET['params']); $var = filter($_GET['params']); $this->filter(); ex: $obj = new Functions(); $obj->filter($param); class test { public function newTest(){ $this->bigTest();// we don't need to create an object we can call simply using $this $this->smallTest(); } private function bigTest(){ //Big Test Here } private function smallTest(){ //Small Test Here } public function scoreTest(){ //Scoring code here; } } $testObject = new test(); $testObject->newTest(); $testObject->scoreTest(); $obj = new Functions(); $var = $obj->filter($_GET['params']); $var = $this->filter($_GET['params']); $f = new Functions; $var = $f->filter($_GET['params']); Functions::filter($_GET['params']); $obj = new Functions(); $var = $obj->filter($_GET['params']); $instance = new Functions(); // create an instance (object) of functions class $instance->filter($data); // now call it Next Lesson PHP TutorialHow do you use an outside class function?C++ class and functions: Outside the class definition
Functions should be declared inside the class to bound it to the class and indicate it as it's member but they can be defined outside of the class. To define a function outside of a class, scope resolution operator :: is used.
How do you call a function in PHP?To invoke a method on an object, you simply call the object name followed by "->" and then call the method. Since it's a statement, you close it with a semicolon. When you are dealing with objects in PHP, the "->" is almost always used to access that object, whether it's a property or to call a method.
What is public function in PHP?public - the property or method can be accessed from everywhere. This is default. protected - the property or method can be accessed within the class and by classes derived from that class. private - the property or method can ONLY be accessed within the class.
Can I define function outside class in Java?Unlike C++, in Java, we cannot have just function declarations in the class and definitions outside of the class.
|