I've looked at debug_backtrace but so far it doesn't do what I need it to do.
I need to know whether the function I'm calling was 'called' or 'echo-ed'. Like this:
function hello() {
//blah blah
}
echo hello(); //echo-ed
hello(); //'called'
But the function would do different things if it was 'called' over 'echo-ed'.
How would I do that?
I am pretty sure that this is impossible. The reason this cannot work is that "echo" or any other operator, function or variable assignment uses the return value of the function you've called. So if you've got the following:
echo function1();
What happens is that function1 gets executed, and the return value is passed to echo. Therefor, function1 cannot possibly know that its return value is going to be "echo-ed", because by the time that happens, function1() has already been called and finished executing.
There is no efficient way to deal it
Update:
There is no way to deal it :)
two examples to help you understand.
function hello(){
return "Hello!";
}
echo hello(); // prints Hello!
function hello(){
echo "Hello!";
}
hello(); // prints Hello!
Related
I've never used my own Wordpress filters before, so I was trying to understand how it's done.
I created a simple example that seems like it should work, but it doesn't. I expect it to echo 'filtered', but it echoes 'unfiltered'. What am I doing wrong here?
function test() {
$stuff = 'unfiltered';
apply_filters('test',$stuff);
echo $stuff;
}
add_filter('test',function() {
return 'filtered';
});
test();
Ahhhhhh. Silly mistake. I needed to assign apply_filter to a variable before echoing it.
<?php
function dosomething(){
echo "do\n";
}
$temp="test".dosomething();
echo $temp;
?>
expected result:testdo
but actual result is:
do
test%
I know how to change the code to get the expected result.
But what i doubt is why the code prints result like this.
Can someone explain it?Thanks!
dosomething is echoing to the screen. Since this runs first "do\n" is printed.
dosomething also doesn't return anything so the second echo is equivalent to echo "test";
In order to use the result of the call you should return it:
function dosomething(){
return "do\n";
}
Which will behave as you expect.
To clarify. In order to work out what $temp is the function must be run first which prints out "do\n" first.
Use return.
function dosomething(){
return "do\n"; }
Use a return statement instead of echo
function dosomething(){
return "do\n";
}
I'm not sure why people are getting down voted. Their answers are right.
http://codepad.org/nagGXY99
Try this:
Use return in the calling function. Doing that you will get the string where the function is being called. So function is being replaced by string and 2 strings will then con cat.
-
Thanks
I have been told that a class cannot be defined within a class in PHP. However, in my own example this seems to work which has me confused:
class_test.php:
require('class_1.php');
new class_1
//Need $missing_variable here.
class_1.php
class class_1{
public function function_1(){
function callback_function(){
echo "A Callback";
$missing_variable = "Where Did I Go?";
}
require('class_2.php');
new class_2('callback_function');
}
public function __construct(){
$this->function_1();
}
}
class_2.php
class class_2{
public function __construct($callback){
echo "Hello World - ";
call_user_func($callback);
}
}
Loading class_test.php prints out
Hello World - A Callback
Question: How do I define $missing_variable such that I can get it where I need it?
In case anyone in the future has a similar problem, however unlikely that may be, I want to link to the codepad from below that shows the $missing_variable echo'd from outside the classes:
http://codepad.org/tRk0XWG7
Thanks again everyone.
Note: This is a follow up.
You can declare a class within a function. That's known as conditional declaration, i.e. only if the function is called will the class be declared. It doesn't make much of a difference then whether you include a file with the class declaration or if you type out the code inside the function.
This does not mean however that the classes share any sort of scope or data. Only the declaration is conditionally nested, it still has the same functionality and scope as explained before.
Your confusion about the callback can be explained by the same thing. When class_1::function_1 is executed the first time, the function callback_function is being defined. This is a regular global function that can be called from anywhere. It's not bound to the class in any way. You will also notice that you cannot execute class_1::function_1 a second time, PHP will complain that callback_function already exists when you're trying to declare it again.
As for the comment in the source code //How do I declare this variable so that it is available where I need it?: You don't. That variable is a local variable inside a function. It's only in scope inside the function. You can return its value from the function like any other return value if you want to. (You could make it global, but for the love of god don't!) If you need that value somewhere else, don't declare it as a variable inside a function, because only the function can access it then.
You would return $missing_variable in a few places. See below. (This isn't the only way to do it, mind you)
http://codepad.org/tf08Vgdx
<?
class class_2{
public function __construct($callback){
echo "Hello World - ";
$missing = $callback();
$this->missing = $missing;
}
}
class class_1{
public function function_1(){
function callback_function(){
echo "A Callback. ";
$missing_variable = "Where Did I Go?";
return $missing_variable;
}
$class2 = new class_2('callback_function');
return $class2->missing;
}
public function __construct(){
$this->missing = $this->function_1();
}
}
$class = new class_1();
echo $class->missing;
Yes, I am quite aware of how horrible Eval() is, I have no choice, it is for an assignment and the professor has instructed us to use it.
I am having trouble figuring out why PHP won't let me call a class's member function from within the class when using Eval(). For example:
$code="class evalTest{
function f1(){
echo 'f1';
f2();
}
function f2(){
echo 'f2';
}
}
$x=new evalTest();
$x->f2();
$x->f1();";
eval($code);
echo 'test';
The above code will print out:
f2f1
and the rest of the page will be blank. It seems like the entire PHP script stops as soon as I call a function from within a function through Eval(). Even the final "echo 'test';" is not printed.
Is there some trick to getting this to work, or is this a limitation of Eval()? Again, I would avoid using Eval() if I could, but we were specifically instructed to use it.
Because you need to call $this->f2(); instead of f2().
You also need to escape $ if you are using double quotes, because any $blah in double-quote strings are interpreted as variable references.
<?php
$code="class evalTest{
function f1(){
echo 'f1';
\$this->f2();
}
function f2(){
echo 'f2';
}
}
\$x=new evalTest();
\$x->f2();
\$x->f1();";
eval($code);
echo 'test';
Demo
use single quotes or protect \$, also you had error where you called f2(), use $this->f2
http://codepad.org/DkQ3PiTQ
$code='class evalTest{
function f1(){
echo "f1";
$this->f2();
}
function f2(){
echo "f2";
}
}
$x=new evalTest();
$x->f2();
$x->f1();;';
eval($code);
echo 'test';
Working a lot with JS I have come to love closures, so I was pleased to learn that there are closures in PHP also. However I just can't get this stuff to work, what's wrong with this piece of code?
class Foo {
public $Bar;
public function Foo() {
$this->Bar = function() { echo "Hello World"; };
}
};
$F = new Foo();
$F->Bar();
I keep getting PHP Fatal error: Call to undefined method Foo::Bar() errors.
This has been discussed a lot on SO already (see e.g. this answer). This should do the trick:
$b = $f->Bar;
$b();
Yes, it is that stupid. You could use call_user_func() to put in in one line (see jlb's answer to this question), but the ugliness remains.
If you want a one-line solution to replace
$F->Bar()
try this:
call_user_func($F->Bar);
PHP has separation between methods and fields. In fact, you can have a method and a field of the same name at the same time:
class Foo {
public $Bar;
function Bar() { echo "hello\n"; }
};
$F = new Foo();
$F->Bar = 42;
$F->Bar(); // echoes "hello"
So you can see that, to avoid ambiguity, there must be a separate syntax between calling a method with that name, and accessing a field with that name and then calling that as a function.
If PHP had better syntax, they would support ($F->Bar)(), i.e. function call operator on any expression, but currently only variables can be "called".
PHP isn't liking the $F->Bar notation for accessing the closure.
If you change this slightly to
$t = $F->Bar();
$t();
then it works.