What is the order of operations with this function call? - php

<?php
class MyClass
{
static function test()
{
echo "Victor";
}
static function result()
{
echo "My name is ".self::test();
}
}
MyClass::result();
?>
I'm confused why self::test() is executed before the rest of the command or the other way around. Thanks in advance for the comments.

Because to get string that needs to be echoed out needs to be "prepared". so before output it needs to know what's the return value of it. it executes first and it's result is included in string. Actually, self::test(); does not return value, but echoes out some text.

Related

PHP function working differently for return and echo. Why?

By using the following class:
class SafeGuardInput{
public $form;
public function __construct($form)
{
$this->form=$form;
$trimmed=trim($form);
$specialchar=htmlspecialchars($trimmed);
$finaloutput=stripslashes($specialchar);
echo $finaloutput;
}
public function __destruct()
{
unset($finaloutput);
}
}
and Calling the function, by the following code, it works fine.
<?php
require('source/class.php');
$target="<script></script><br/>";
$forminput=new SafeGuardInput($target);
?>
But if in the SafeGuardInput class if I replace echo $finaloutput; with return $finaloutput; and then echo $forminput; on the index.php page. It DOES NOT WORK. Please provide a solution.
You can't return anything from a constructor. The new keyword always causes the newly created object to be assigned to the variable on the left side of the statement. So the variable you've used is already taken. Once you remember that, you quickly realise there is nowhere to put anything else that would be returned from the constructor!
A valid approach would be to write a function which will output the data when requested:
class SafeGuardInput{
public $form;
public function __construct($form)
{
$this->form=$form;
}
public function getFinalOutput()
{
$trimmed = trim($this->form);
$specialchar = htmlspecialchars($trimmed);
$finaloutput = stripslashes($specialchar);
return $finaloutput;
}
}
Then you can call it like in the normal way like this:
$obj = new SafeGuardInput($target);
echo $obj->getFinalOutput();

Does the function run when assigned to local variable

Below is the example I put together to understand my confusion. Now my question is, when I make the function into a local variable, does it start to run instantly or does it wait for the local variable to be called.
//Here is the function
function myFunction(){
return 'Hello Stackoverflow';
}
//Does the functio run at this point
$stackoverflow = myFunction();
//Or does the function run here?
echo $stackoverflow;
You aren't making "the function into a local variable."
In your example the function runs, the string return value is assigned to $stackoverflow, then you echo the string.
I think you are trying to do this.
//Here is the function
function myFunction(){
return 'Hello Stackoverflow';
}
//Doesn't run yet
$stackoverflow = 'myFunction';
//This runs now
echo $stackoverflow();
You are not actually assigning a function to the variable, but the return value of the function.
And yes, the function is executed when you call it, i.e. when you assign it to the variable.
After that of course you have a variable with a value and you can do whatever you want with it.
It will run at the time of assignment:
$stackoverflow = myFunction();
You could assign the function to the variable if you want it called when referencing the variable and not when it's assigned:
$foo = function () {
return 'Hello Stackoverflow';
};
echo $foo();
The function run when it is assigned to a value, i.e, at $stackoverflow = myFunction();

php calling a function using a string

i have below a function called test thats being called, and just echos "test" keeping it simple, for this question.
test();
function test() {
echo "do something";
}
However i want to try and make the function dynamic if thats the right choice of words.
I have a list of records in a database, and based on each record, i may want a different function to process that record, the idea then is to have a field in the database which would be function_name, i then use that field to some how call that function.
Like this
test();
$function_name = "test";
function $function_name() {
echo "do something here";
}
the only way i can think to get around this is to use switch/case but that is going to cause me other issues down the line.
The function has to be defined with a specific name but you can call it using a variable that contains its name like so :-
<?php
function name() {
echo "name";
}
$func_name = 'name';
// its always a good idea to check that function
// actually exists before calling it.
if (function_exists($func_name)) {
$func_name();
}
Use closures for this:
<?php
$funcName = 'test';
$$funcName = function ()
{
echo 'Do something';
};
$test(); // 'Do something'
$$funcName(); // 'Do something'

PHP return string without "echo"?

I have a class containing a lot of "return" functions :
Class Components_superclass
{
public function build()
{
$this->add_category('Grille');
}
public function add_category($name)
{
return '<div class="category">'. $name .'</div>';
}
...
}
I want to get the html code containing in "add_category" function. But when I echo this, I have nothing :
$component = new Components_superclass();
echo $component->build();
Must I add "return" in build function ? Is there a way to avoid this ? Because I have a lot of function to call and I don't want to write something like this :
public function build()
{
return
$this->function_1() .
$this->function_2() .
$this->function_3();
}
Thanks !
Yes, the echo doesn't work because nothing is returned from build – there is not string that's passed into echo which could be printed.
About your second question, you could buffer the string internally and then return it at once, like this:
Class Components_superclass
{
private $buffer = array();
// …
public function add_category($name)
{
$this->buffer[] = '<div class="category">'. $name .'</div>';
}
public function output()
{
return implode('', $this->buffer);
}
}
If you want a function to return a value (that can be a string, integer or other types) use return. If you call the function, the returned value is then available on that place.
If function_1() return the string 'I am function one' and you echo the function call (echo $this->function_1();), the string 'I am function one' will be echoed.
This is also the correct way of working with functions. If you want to echo thing from inside the function, just echo in the function.
Check out PHP.net's function documentation!

Closure behaving unexpectedly

I have a closure inside a function but when it is called the return value is not where i expected it to be.
public function test($name, $content)
{
$test = "\t<div id=\"{$name}\">{$content()}</div>\n";
return $test;
}
Instead of returning this...
<div id="name">content</div>
It instead returns...
content
<div id="name"></div>
If you have any idea how to fix this to display properly then i would be a very happy man, thanks in advance.
You haven't showed the $content() function, but from the symptoms I assume it prints the content instead of returning it. What happens is that the test() function calls the $content() function which displays the content and returns nothing, then test() returns and something else prints the return value.
To fix it simply have $content() return the content instead of printing it.
Let the closure execute and catch what's returned. Then insert the result:
public function test($name, $content)
{
$insert = $content();
$test = "\t<div id=\"{$name}\">{$insert}</div>\n";
return $test;
}

Categories