Look this example code:
I'm getting the error:
Fatal error: Uncaught Error: Cannot use object of type Closure as array in C:\xampp\htdocs\dermaquality\test.php:11 Stack trace: #0 C:\xampp\htdocs\dermaquality\test.php(20): test(Object(Closure)) #1 {main} thrown in
$array = array(
'hello' => function() {
echo "HEllo world";
}
);
function test( $func )
{
if (is_callable( $func['hello'] )) {
$func['hello']();
}
else {
$func();
}
}
echo "Executing 1 <br/>";
test( $hello = function() {"Hello world";} );
echo "Executing 2 <br/>";
test( $array['hello'] );
exit;
How can I call $func if $func is function or if $func['hello'] is function?
Thanks.
Problem is in if (is_callable( $func['hello'] )) { because you dont know if $func is an array.. btw you dont put array as parameter in test( $array['hello'] ); you put just function...
function test( $func )
{
if (is_callable($func)) {
$func();
}
else if (is_array($func)){
if(isset($func['hello']) && is_callable($func['hello'])){
$func['hello']();
}else{
// unknown what to call
}
}else{
// unknown what to call
}
}
Related
Hello I have a connection problem with my bdd because it does not want to connect. In my error it says that it's a sql query problem, I've looked for it but I can't find it...
I think it is my function query(Table.php) which is not good and therefore my function prepare(Databse.php) which does not validate it.
error:
Fatal error: Uncaught TypeError: PDOStatement::execute(): Argument #1 ($params) must be of type ?array, string given in C:\xampp\htdocs\gra\app\Database.php:61 Stack trace:
#0 C:\xampp\htdocs\gra\app\Database.php(61): PDOStatement->execute('App\Table\Artic...')
#1 C:\xampp\htdocs\gra\app\Table\Table.php(36): App\Database->prepare(' SELECT article...', 'App\Table\Artic...', 'App\Table\Artic...', false)
#2 C:\xampp\htdocs\gra\app\Table\Article.php(18): App\Table\Table::query(' SELECT article...', 'App\Table\Artic...')
#3 C:\xampp\htdocs\gra\pages\home.php(12): App\Table\Article::getLast()
#4 C:\xampp\htdocs\gra\public\index.php(28): require('C:\xampp\htdocs...')
#5 {main} thrown in C:\xampp\htdocs\gra\app\Database.php on line 61
// My prepare function of my Database.php page`
public function prepare($statement, $attributes, $class_name, $one = false){
$req = $this->getPdo()->prepare($statement);
$req->execute($attributes);
$req->setFetchMode(PDO::FETCH_CLASS, $class_name);
if($one){
$datas = $req->fetch();
}else {
$datas = $req->fetchAll();
}
return $datas;
}
//Function query of my Table.php page
public static function query($statement, $attributes = null, $one = false ){
if($attributes){
return App::getDB()->prepare($statement, $attributes, get_called_class(), $one);
} else {
return App::getDB()->query($statement, get_called_class(), $one);
}
}
Here is my github:
https://github.com/xavgdt66/grablog/tree/main/gra
I tried to put execute but it doesn't work.
How to solve this problem with call_user_func
When I call the function I have this error generated by php. Below the error.
Warning: call_user_func() expects parameter 1 to be a valid callback, function 'getChildsInMenuCount' not found or invalid function name i
the line inside my files with the function
while ($Qcategories->fetch() ) {
$categories_count++;
$rows++;
if ((!isset($_GET['cID']) && !isset($_GET['pID']) || (isset($_GET['cID']) && ((int)$_GET['cID'] === $Qcategories->valueInt('id')))) && !isset($cInfo) && (substr($action, 0, 3) != 'new')) {
$category_childs = ['childs_count' => AdministratorMenu::getChildsInMenuCount($Qcategories->valueInt('id'))];
$cInfo_array = array_merge($Qcategories->toArray(), $category_childs);
$cInfo = new objectInfo($cInfo_array);
}
The result of var_dump(__FUNCTION__); is string(20)"getChildsInMenuCount"
class AdministratorMenu {
// Count how many subcategories exist in a category
public static function getChildsInMenuCount($id) {
$OSCOM_Db = Registry::get('Db');
$categories_count = 0;
$Qcategories = $OSCOM_Db->prepare('select id
from :table_administrator_menu
where parent_id = :parent_id
');
$Qcategories->bindInt(':parent_id', $id );
$Qcategories->execute();
while ($Qcategories->fetch() !== false) {
$categories_count++;
$categories_count += call_user_func(__FUNCTION__, $Qcategories->valueInt('id'));
}
return $categories_count;
}
}
Since it's a class method, you need to use __METHOD__, not __FUNCTION__. This will include the class prefix.
$categories_count += call_user_func(__METHOD__, $Qcategories->valueInt('id'));
change call_user_func parameters like below:
call_user_func(__CLASS__ . '::' . __FUNCTION__, $Qcategories->valueInt('id'));
You must also specify the class in first parameter of call_user_func
public function update($table, $where = array(), $data_arr = array()){
print_r($data_arr);
$adapter = $this->tableGateway->getAdapter();
$projectTable;
if($table != null){
$projectTable = new TableGateway($table, $adapter);
}else{
$projectTable = new TableGateway('account_master', $adapter);
}
echo "158";
try {
echo "123";
$rowset = $projectTable->update(function(Update $update) use ($where, $data_arr) {
$update->set(array('statement_no' => '01010'));
$update->where($where);
echo $update->getSqlString();
});
} catch (\Exception $e) {
print_r($e);
}
print_r($rowset);
die();
}
my Output print : 158123
it's give me pass array in set() function that i already pass as argument. also i have tried to convert object to array ((arrya)$objetc) but it's not work for me.
[10-Jul-2017 05:11:34 America/Denver] PHP Catchable fatal error: Argument 1 passed to Zend\Db\Sql\Update::set() must be of the type array, object given, called in /home2/flywing1/vendor/zendframework/zend-db/src/TableGateway/AbstractTableGateway.php on line 336 and defined in /home2/flywing1/vendor/zendframework/zend-db/src/Sql/Update.php on line 93
You may do that by implementing a Zend\Db\Sql\Update object. You may create that object using the TableGateway. You should be able to do the following in your model
public function update($set, $where)
{
// Here is the catch
$update = $this->tableGateway->getSql()->update();
$update->set($set);
$update->where($where);
// Execute the query
return $this->tableGateway->updateWith($update);
}
Try it,
I was with the same issues, I tried with this, and it worked.
$rowset = $projectTable->update(array('statement_no' => '01010'), $where);
Whats wrong with my codes, I always got fatal error when I change my URL. I can't remove those if else below which have undefined, I will not get 2nd URL. I am beginner creating MVC please help
class event_trap
{
function __construct()
{
$url = $_GET['url'];
$url = rtrim($url. '/');
$url = explode('/', $url);
//print_r($url);
$file = 'event_mvc/controllers/' .$url[0] . '.php';
if (file_exists($file)) {
require $file;
} else {
require 'event_mvc/controllers/error.php';
$controller = new Error();
return false;
}
$controller = new $url[0];
if (isset($url[2])) {
$controller->{$url[1]}($url[2]); //<-- Undefined method
} else {
if(isset($url[1])) {
$controller -> {$url[1]}(); //<-- Undefined method
} else {
}
}
}
}
var_dump($url)
array (size=2)
0 => string 'event' (length=5)
1 => string '' (length=0)
Fatal error: Call to undefined method event::() in
C:\wamp\www\tabulation\event_mvc\libs\Bootstrap.php on line 33
Call Stack
# Time Memory Function Location
1 0.0004 142728 {main}( ) ..\event.php:0
2 0.0012 149096 event_trap->__construct( ) ..\event.php:4
Your variable $url is an array which you use to create an object and call methods with or without parameters. With the array ou have var_dumped 0=>event, 1=>'' you are creating an object of class event and call the method '' which does not exist. Correct your code or pass a valid function name.
You can try this which checks for empty string too.
if (isset($url[2]) && !empty($url[2]) {
$controller->{$url[1]}($url[2]);
} else {
if(isset($url[1]) && !empty($url[1]) {
$controller -> {$url[1]}();
}
}
This question already has answers here:
Get name of caller function in PHP?
(13 answers)
Closed 9 years ago.
I have the following code:
function abcdef() { }
function test($callback) {
// I need the function name string("abcdef") here?
}
test(abcdef);
Is it possible to get the function name within the test function?
And what about anonymous functions?
This has been asked before: How can I get the callee in PHP?
You can get the information you need with debug_backtace. Here is a very clean function I have found:
<?php
/**
* Gets the caller of the function where this function is called from
* #param string what to return? (Leave empty to get all, or specify: "class", "function", "line", "class", etc.) - options see: http://php.net/manual/en/function.debug-backtrace.php
*/
function get_caller($what = NULL)
{
$trace = debug_backtrace();
$previousCall = $trace[2]; // 0 is this call, 1 is call in previous function, 2 is caller of that function
if(isset($what)) {
return $previousCall[$what];
} else {
return $previousCall;
}
}
And you (might) use it like this:
<?php
function foo($full)
{
if ($full) {
return var_export(get_caller(), true);
} else {
return 'foo called from ' . get_caller('function') . PHP_EOL;
}
}
function bar($full = false)
{
return foo($full);
}
echo bar();
echo PHP_EOL;
echo bar(true);
Which returns:
foo called from bar
array (
'file' => '/var/www/sentinel/caller.php',
'line' => 31,
'function' => 'bar',
'args' =>
array (
0 => true,
),
)
You can try with function.name :
function abcdef() { }
function test($callback) {
alert($callback.name)
}
test(abcdef);