This question already has answers here:
Calling closure assigned to object property directly
(12 answers)
Closed 9 years ago.
I have php code like:
class Foo {
public $anonFunction;
public function __construct() {
$this->anonFunction = function() {
echo "called";
}
}
}
$foo = new Foo();
//First method
$bar = $foo->anonFunction();
$bar();
//Second method
call_user_func($foo->anonFunction);
//Third method that doesn't work
$foo->anonFunction();
Is there a way in php that I can use the third method to call anonymous functions defined as class properties?
thanks
Not directly. $foo->anonFunction(); does not work because PHP will try to call the method on that object directly. It will not check if there is a property of the name storing a callable. You can intercept the method call though.
Add this to the class definition
public function __call($method, $args) {
if(isset($this->$method) && is_callable($this->$method)) {
return call_user_func_array(
$this->$method,
$args
);
}
}
This technique is also explained in
JavaScript-style object literals
Related
This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 6 years ago.
I m searching for some help
It is possible to set a function with Arguments/Parameters inside another function in php? Here i have a theoretical example.
<?php
// Function with parameter/s
function funcOne($arg) {
return $arg;
}
// Parameter/s inside another function.. Possible!?
function funcTwo() {
return funcOne($arg);
}
When i try to set the parameter like this
funcOne('Alex');
echo funcTwo();
I get the following notice error
Notice: Undefined variable: arg in...
Thanks in advance :)
// Function with parameter/s
function funcOne($arg) {
return $arg;
}
// Parameter/s inside another function.. Possible!?
function funcTwo() {
return funcOne($arg);
}
funcOne('Alex');
//Call is made to function one which returned an ARG.
NOTE that here the function just returned the arg and forgot about it, now that argument is NO WHERE stored to be used
//Now here inside the functionTwo scope $arg is never defined.
echo funcTwo();
You may do the following using classes and objects
class MyClass {
public $classarg;
public function funcOne($arg) {
$this->classarg = $arg; //assigned the argument to a class variable
}
function funcTwo() {
return $this->classarg; //using the class variable to test
}
}
$myobj = new MyClass();
$myobj->funcOne('Alex');
echo $myobj->funcTwo()
You can also use global variable to achieve what you want, but I will NOT recommend to use it as Object Oriented Programming is what we should be using going forward
funcOne('Alex')is not setting a parameter, it is calling the function funcOne().
When funcOne($arg) executes, it returns the parameter $arg to the caller.
echo funcOne('Alex') will echo Alex, because that is the value returned.
After return, funcOne does not know about 'Alex' any more.
when you call funcTwo(), it executes funcOne($arg), but $arg is not defined: it has no value assigned.
function funcTwo($arg) {
return funcOne($arg);
}
Note that you should learn to use variables before making functions.
This question already has answers here:
PHP method chaining or fluent interface?
(10 answers)
Closed 7 years ago.
I see in codeigniter a sintaxis method->other_method->other_method_again. example:
$this->db->select()->join()->findAll();
When i try
class MyClass{
public function bla(){
echo "bla";
}
public function other(){
echo "other";
}
}
$message = new MyClass();
$message->bla()->other();
Return:
Fatal error: Call to a member function other()
as I can do what codeigniter?
this is called method chaining (popularised by jQuery chaining) and is achieved if you
return $this
from each method
for your example:
class MyClass{
public function bla(){
echo "bla";
return $this; // enable method chaining
}
public function other(){
echo "other";
return $this; // enable method chaining
}
}
The reason this works is the same the following works:
$instance->method1();
$instance->method2();
Here each method is called on an $instance but if each method returns the actual $instance back which is the same as $this, then one can combine the statements like this ("chain" them):
$instance->method1()/* returns $instance and can be used again*/->method2();
That's all there is to it.
This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 7 years ago.
<?php
class Door
{
public function __construct()
{
}
public function test(){
echo "welocme";
}
}
$obj=new Door();
get_data();
function get_data(){
$obj->test();
}
$obj->test(); work well outside function but i need inside function. I cannot access object inside function show error
Fatal error: Call to a member function test()
Try like this: it may work..
If u use any outer variable in a function, then decleare as global $use_variable_name . now u can understand...
function get_data(){
global $obj;
$obj->test();
}
another and better way:
get_data($obj);// call this way...
function get_data($object){
$object->test();
}
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Calling closure assigned to object property directly
If I have a class like this:
class test{
function one(){
$this->two()->func(); //Since $test is returned, why can I not call func()?
}
function two(){
$test = (object) array();
$test->func = function(){
echo 'Does this work?';
};
return $test;
}
}
$new = new test;
$new->one(); //Expecting 'Does this work?'
So my question is, when I call function two from function one, function two returns the $test variable which has a closure function of func() attached to it. Why can I not call that as a chained method?
Edit
I just remembered that this can also be done by using $this->func->__invoke() for anyone that needs that.
Because this is currently a limitation of PHP. What you are doing is logical and should be possible. In fact, you can work around the limitation by writing:
function one(){
call_user_func($this->two()->func);
}
or
function one(){
$f = $this->two()->func;
$f();
}
Stupid, I know.
This question already has answers here:
PHP method chaining or fluent interface?
(10 answers)
Closed 7 years ago.
jQuery lets me chain methods. I also remember seeing the same in PHP so I wrote this:
class cat {
function meow() {
echo "meow!";
}
function purr() {
echo "purr!";
}
}
$kitty = new cat;
$kitty->meow()->purr();
I cannot get the chain to work. It generates a fatal error right after the meow.
To answer your cat example, your cat's methods need to return $this, which is the current object instance. Then you can chain your methods:
class cat {
function meow() {
echo "meow!";
return $this;
}
function purr() {
echo "purr!";
return $this;
}
}
Now you can do:
$kitty = new cat;
$kitty->meow()->purr();
For a really helpful article on the topic, see here: http://www.talkphp.com/advanced-php-programming/1163-php5-method-chaining.html
Place the following at the end of each method you wish to make "chainable":
return $this;
Just return $this from your method, i.e. (a reference to) the object itself:
class Foo()
{
function f()
{
// ...
return $this;
}
}
Now you can chain at heart's content:
$x = new Foo;
$x->f()->f()->f();
yes using php 5 you can return object from a method. So by returning $this (which points to the current object), you can achieve method chaining