This question already has answers here:
Sharing var from one function to the other function in PHP Class
(2 answers)
Closed 4 years ago.
Let's say I have two different functions, and one of them has a defined variable. In the second function, I don't wanna write the same variable again, can I simply use the variable from the first function in the second one WITHOUT redefining it in the second function?
Someting like:
class example{
public function a($foo){
$foo2 = $foo + 1
return $foo2;
}
public function b($foo2){
echo "result: " . $foo2;
}
}
It's simple, you can use a property $foo2 and access it from both methods:
class example{
private $foo2;
public function a($foo){
$this->foo2 = $foo + 1
return $this->foo2;
}
public function b(){
echo "result: " . $this->foo2;
}
}
$obj = new example();
$obj->a(5);
$obj->b(); // result: 6
Related
This question already has answers here:
How to call a closure that is a class variable?
(2 answers)
Closed 4 years ago.
$f = function($v) {
return $v + 1;
}
echo $f(4);
// output -> 5
The above works perfectly fine. However, I cannot reproduce this correctly when f is a property of a class.
class MyClass {
public $f;
public function __construct($f) {
$this->f = $f;
}
public function methodA($a) {
echo $this->f($a);
}
}
// When I try to call the property `f`, PHP gets confused
// and thinks I am trying to call a method of the class ...
$myObject = new myClass($f);
$myObject->methodA(4);
The above will result in an error:
Call to undefined method MyClass::f()
I think the problem is that it is trying to make sense of
echo $this->f($a);
And as you've found it wants to call a member function f in the class. If you change it to
echo ($this->f)($a);
It interprets it as you want it to.
PHP 5.6
Thanks to ADyson for the comment, think this works
$f = $this->f;
echo $f($a);
While Nigel Ren's answer (https://stackoverflow.com/a/50117174/5947043) will work in PHP 7, this slightly expanded syntax will work in PHP 5 as well:
class MyClass {
public $f;
public function __construct($f) {
$this->f = $f;
}
public function methodA($a) {
$func = $this->f;
echo $func($a);
}
}
$f = function($v) {
return $v + 1;
};
$myObject = new myClass($f);
$myObject->methodA(4);
See https://eval.in/997686 for a working demo.
This question already has answers here:
How to get name of calling function/method in PHP? [duplicate]
(10 answers)
Closed 7 years ago.
Is there any way (dirty is fine, this is only for debugging) to get the __FUNCTION__ (function name) from where a function was called from?
example;
function a() {
//code
return b();
}
function b() {
return __PARENT_FUNCTION__
}
echo a();
This should yield a as the function name that was originally called for this output
Take a look at debug_backtrace
function a() {
//code
return b();
}
function b() {
return debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)[0]["function"];
}
echo a();
This question already has answers here:
Get variable name of object inside the object php
(3 answers)
Closed 8 years ago.
I would like to know, if it is possible in PHP (using reflection or not) to get the variable name abc inside the class method in this example.
class Example
{
public function someMethod()
{
// once this method is called, I want it to echo `abc` in this example
}
}
Now, when I call the method using a variable name like
$abc= (new Example)->someMethod();
echo $abc; // abc
I would like to see the name of the variable, 'foo' shown, in other words the class would have to be aware of the variable name, when returning the methods contents.
I always pass in the name of the variable it will be assigned to if it it is required
class myclass {
var $myname;
function __construct($myname='no name') {
$this->myname=$myname;
#print "In BaseClass constructor\n";
}
function sayHello()
{
return "hello from " . $this->myname . "\n";
}
}
usage:
$myVar = new myclass("myVar");
$yourVar = new myclass("yourVar");
echo $myVar->sayHello();
echo $yourVar->sayHello();
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:
How do I get the function name inside a function in PHP?
(4 answers)
Closed 8 years ago.
Is there a function that can return the name of the current function a program is executing?
Yes, you can get the function's name with the magic constant __FUNCTION__
class foo
{
function print_func()
{
echo __FUNCTION__;
}
function print_method()
{
echo __METHOD__;
}
}
$obj = new foo();
$obj->print_func(); // Returns: print_func
$obj->print_method(); // Returns: foo::print_method
Maybe via debug_backtrace http://www.php.net/manual/en/function.debug-backtrace.php