How to reproduce the error, and how to fix it:
Put this code in a file called
p.php:<?php class yoyo{ function salt(){ } function pepper(){ salt(); } } $y = new yoyo(); $y->pepper(); ?>Run it like this:
php p.phpWe get error:
PHP Fatal error: Call to undefined function salt() in /home/el/foo/p.php on line 6Solution: use
$this->salt();instead ofsalt();So do it like this instead:
<?php class yoyo{ function salt(){ } function pepper(){ $this->salt(); } } $y = new yoyo(); $y->pepper(); ?>
If someone could post a link to why $this has to be used before PHP functions within classes, yeah, that would be great.
Answer from Eric Leschinski on Stack OverflowHow to reproduce the error, and how to fix it:
Put this code in a file called
p.php:<?php class yoyo{ function salt(){ } function pepper(){ salt(); } } $y = new yoyo(); $y->pepper(); ?>Run it like this:
php p.phpWe get error:
PHP Fatal error: Call to undefined function salt() in /home/el/foo/p.php on line 6Solution: use
$this->salt();instead ofsalt();So do it like this instead:
<?php class yoyo{ function salt(){ } function pepper(){ $this->salt(); } } $y = new yoyo(); $y->pepper(); ?>
If someone could post a link to why $this has to be used before PHP functions within classes, yeah, that would be great.
This was a developer mistake - a misplaced ending brace, which made the above function a nested function.
I see a lot of questions related to the undefined function error in SO. Let me note down this as an answer, in case someone else have the same issue with function scope.
Things I tried to troubleshoot first:
- Searched for the php file with the function definition in it. Verified that the file exists.
- Verified that the require (or include) statement for the above file exists in the page. Also, verified the absolute path in the require/include is correct.
- Verified that the filename is spelled correctly in the require statement.
- Echoed a word in the included file, to see if it has been properly included.
- Defined a separate function at the end of file, and called it. It worked too.
It was difficult to trace the braces, since the functions were very long - problem with legacy systems. Further steps to troubleshoot were this:
- I already defined a simple print function at the end of included file. I moved it to just above the "undefined function". That made it undefined too.
Identified this as some scope issue.
Used the Netbeans collapse (code fold) feature to check the function just above this one. So, the 1000 lines function above just collapsed along with this one, making this a nested function.
Once the problem identified, cut-pasted the function to the end of file, which solved the issue.
$index = array_search($key, array_map(function ($item) {
return $item['name'];
}, $tmp));
Requires PHP5.3
Or you can use array_keys() (works in pre-5.3 too)
$indexs = array_keys($tmp, array('name' => $key));
$indexs is an array now, because there can be of course more than one index with the value array('name' => $key) within $tmp
You could run an array_map on your array with a callback that examines each element and tests for the value you are looking for, though you'd still need to keep an internal pointer to the index you are currently processing, so I suggest going with the foreach loop anyways.
Probably the PHPUnit version you're using is not up-to-date yet for PHP 8 try
while (list($i, $arg) = each($args)) {
into
foreach ($args as $i => $arg) {
not an expert of PHPUnit but the "each" function is not available anymore in PHP 8
Warning: This function has been DEPRECATED as of PHP 7.2.0, and REMOVED as of PHP 8.0.0. Relying on this function is highly discouraged.
Taken from the PHP site
Probably the PHPUnit version you're using is not up-to-date yet for PHP 8. Check the version if you can and then see here PHPUnit version support
Hello i have this issue but im not figured out why
i have this in cart.php
function deleteCart($id){
foreach($_SESSION["cart"] as $keys => $values) {
if($values["item_id"] == $_GET["skuid"]) {
unset($_SESSION["cart"][$keys]);
echo"Item Removed";
}
}
}and in the sime file i have
function buyCart(){
code....
if( $row==0){
code...
}else{
$id=1;
deleteCart($id);
}
}why???
This appears to be a breaking change in Laravel 6.0
5.6 - Uses the following
array = ['products' => ['desk' => ['price' => 100]]];
$price = array_get($array, 'products.desk.price');
6.0 - Uses the following
$array = ['products' => ['desk' => ['price' => 100]]];
$price = Arr::get($array, 'products.desk.price');
https://laravel.com/docs/6.0/helpers#method-array-get
https://laravel.com/docs/5.6/helpers#method-array-get
It looks like this call is only used in 3 places in the codebase:
https://github.com/FaCuZ/laravel-theme/search?q=array_get&unscoped_q=array_get
Answer: Try update the calls in the package to match 6.0 ( Assuming there is no other breaking changes) this should work. If it works im sure a lot of people would be thankful for the pull request.
Laravel 6.x and 7.x uses Arr::get() equivalent to array_get().To use it add the array facade on the top of your controller or php file
use Illuminate\Support\Arr;
use Illuminate\Support\Arr;
$array = ['products' => ['desk' => ['price' => 100]]];
$price = Arr::get($array, 'products.desk.price');
For more info on laravel 6.x arrays and helpers