ajax response inside a function by ob clean - php

I'm looking for a way to calling a function by a function and get it's content executed in the function like:
function response($execute){
ob_clean();
$execute();
die();
}
so when i call, i want to give it a process as argument, like:
response(echo("hi"));

PHP using anonymous function.
PHP code demo
function response($execute)
{
if(is_callable($execute))
{
$execute("some-value");
}
else
{
echo "Not a function";
}
}
response(function($someVariable){
echo "Hi i am in anonymous function with 1st argument ".$someVariable;
});
response("Hi");

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();

What is the use of a variable function in PHP?

I understand how to implement a variable function though i don't understand it's use. Why call a function using a variable than to call the function itself?
Unless to dynamically call functions from user input or returned database results?
EXAMPLE : if you have an input like /?do=something
require_once('do.php');
$fun = 'do_'.$_GET['do'];
if (function_exists($fun)) {
$fun(); //variable function
} else {
not_found();
}
so in this case I just add a function to my do.php file and it will be ready to use
do.php :
<?php
function do_getkey() {
// do something when do=getkey
}
function do_sendkey() {
// do something when do=sendkey
}
function not_found() {
// when not found
}
?>

PHP/Laravel How to include one function into another?

Is there a way I can include(?) one function into another? For example, the same way we can include files using include function.
Thank you.
<?php
class test{
public function message1(){
$message = 'i am in message1 function';
return $message;
}
public function message2(){
$message = $this->message1();
echo $message;
}
}
Functions can not be "included" like you mean but you can call them and use their returned values to other functions like below.
Now if you try to call the message2 function using something like:
$messageClass = new test();
echo $messageClass->message2();
you will see that the output is the $message from function message1
Do you mean callback function? If so, this is how to use it:
// This is callback function which will passed as argument to another function.
function callbackFunction1 ($str) {
return strtoupper($str);
}
function mainFunction ($offeredCallback, $str) {
echo( "(" . $offeredCallback($str) . ")<br>");
}
mainFunction("callbackFunction1", "foo");
// Output "(foo)".
// If you want to use Variable Function, define it like this:
$callbackFunction2 = function ($str) {
return strtoupper($str);
};
mainFunction($callbackFunction2, "bar");
// Output "(bar)".
About Variable Function, see Anonymous Function.

Call function and pass parameter over url PHP

This is my PHP script:
<?php
function myFunc($param1)
{
echo $param1;
}
if(isset($_GET['action'])){
if(function_exists($_GET['action'])) {
$_GET['action']();
}
}
?>
Now I want call this function from other php and pass parameter:
http://localhost/data.php?action=myFunc
How to pass parameter to myFunc through url?
It is most certainly wrong and insecure (imagine what would happen if you tried calling it with action=unlink&param=somefile.php), but you could do something like:
With URL: http://localhost/data.php?action=myFunc&param=123
<?php
function myFunc($param1)
{
echo $param1;
}
if(isset($_GET['action'])){
if(function_exists($_GET['action'])) {
$_GET['action']($_GET['param']);
}
}

Check the method was called in php

I have following php code in basic.php. How can I determine if the method row() was called? When I write next <?php $basic->row(); ?> it shows something like this - the method was defined!
Example
$basic->container(); // container was called
$basic->container(); // when called again, i need show some warning - CONTAINER CAN BE PUT ONLY ONCE and using exit() for example
This is the solution what i need
public function container(){
static $container = false;
if ( $container ){ return; } else { print '<div class="container">'; } $container = true;
}
If you want to know if an object has a method or not before calling it, you can use method_exists.
if(method_exists($basic, 'row')) {
$basic->row();
}
Use the echo construct inside the row function like this:
// your code
function row(){
echo "Function called!";
....
}
This will print the text "Function called!" everytime you call the function.
I just add it as an answer ... try smthg like this:
function row(){
if($wascalled === true) {
echo "The function was called";
}
//your Code here
$wascalled = true;
}
So, the first time you call the function, nothing happens, if you call it more, the message will appear. It looks ugly and i dont see much sense in it, but it seems to work.
I did not understand if you mean really a call or if the method is defined.
As #Prasanth said, if you mean if the method is defined - method_exists will be a solution.
Otherwise, you can check my answer here: Cahining pattern
It's related to your problem, as you need a generic way to register a method been called before.
You, ofcourse, can write down
public $_row = false;
public function row() {
$this->_row = true;
// some stuff
}
and later:
if (!$basic->_row) {
$basic->row();
}
You just need a property where you will set a value, which corresponds to your script later. I.e. here the default value is false - it means the method hasn't been called yet. Once method is called, it changes it to true. You are testing if the value is default (false) then call.
You may not change the value to true, but to the string you wanted. E.g. $this->_row = 'the method was defined!' Or set to true and print the string, if $this->_row == true.
Reference
bool function_exists ( string $function_name )
Parameters The $function_name, as a string.
Returns TRUE if function_name exists and is a function, FALSE otherwise.
Note:
This function will return FALSE for constructs, such as include_once and echo.
<?php
if (function_exists('function_name')) {
echo "IMAP functions are available.<br />\n";
} else {
echo "IMAP functions are not available.<br />\n";
}
?>
Take this as an example
<?php
if (function_exists('foo')) {
print "foo defined\\n";
} else {
print "foo not defined\\n";
}
function foo() {}
if (function_exists('bar')) {
print "bar defined\\n";
} else {
print "defining bar\\n";
function bar() {}
}
print "calling bar\\n";
bar(); // ok to call function conditionally defined earlier
print "calling baz\\n";
baz(); // ok to call function unconditionally defined later
function baz() {}
qux(); // NOT ok to call function conditionally defined later
if (!function_exists('qux')) {
function qux() {}
}
?>
Prints:
foo defined
defining bar
calling bar
calling baz
PHP Fatal error: Call to undefined function qux()
Alternative method
You can use magic method __call.
class Basic{
function row(){
print ' Call method '.__METHOD__.'<br/>';
}
function __call($method,$params){
if (method_exists($this, $method)) {
call_user_func_array(array($this, $method), $params);
}else{
print 'Class '.__CLASS__.' hasn`t method "'.$method.'"<br/>';
}
}
}
$basic = new Basic();
$basic->row();
$basic->fetch();
// output
Call method Basic::row
Class Basic hasn`t method "fetch"

Categories