Anonymous functions not working in PHP 5.5.x - php

So, here is a small code snippet from my project
if (!empty($user['IndustryUser'])) {
$user['IndustryUser'] = array_filter($user['IndustryUser'], function ($ku) {
$k = !empty($ku['Industry']['name']) ? trim($ku['Industry']['name']): '';
return $k != '';
});
$userIndustries = array();
foreach ($user['IndustryUser'] as $ku) {
$userIndustries[] = $ku;
}
$user['IndustryUser'] = $userIndustries;
}
This code is present inside a Model named User.php. And also there is a shell script named FetchIndustriesShell.php inside app/console/command, which makes use of this Model.
When I run this script from the command line, as cake FetchIndustries, I get the following error
Parse error: syntax error, unexpected T_FUNCTION in D:\wamp\www\industry-svn\
app\Model\User.php on line 203
where Line 203, is where the anonymous function is defined(line 2 from code snippet).
What i tried is
1) Run phpinfo() inside bootstrap.php to verify the php version. It shows PHP 5.5.12 (attached is the screenshot).
2) I installed xampp,to verify if my wampp installtion/php libraries had problem. But I still got the same error.
3) Also referred https://github.com/cakephp/debug_kit/issues/134. But that did not help either.
CakePHP Version used is: 2.5.1
Project currently running on: wampp (local)

Related

foreach on null throws error since migration to Symfony 3

I migrated my web application from Symfony 2 to 3. Since then whenever a foreach loop is executed on null, an error is thrown ("Invalid argument").
Example:
$myResults = null;
foreach ($myResults as $item) {
// do something
}
How do I achieve that as before no error is thrown?
This is a bug introduced by an upgrade to a newer PHP version (7.1+).
You can verify this by executing your code sample in an interactive php shell without any Symfony code being loaded:
$ php -a
Interactive shell
php > $var = null;
php > foreach ($var as $v) {}
PHP Warning: Invalid argument supplied for foreach() in php shell code on line 1
You can also verify this on 3v4l.org
In order to fix this you have to check for null before looping or (temporarily) go back to an older (unsupported!) PHP version. Depending on how well your code is typed there are some static code analysis tools, that will help you finding the loops you will have to safeguard.

How to get syntax error form php exec

I use this to compile my java file
$command_compile = "javac $target_path_file 2>&1";
exec($command_compile, $compile_result, $return_compile);
And When my java file has syntax error. It always has error
Error: Could not find or load main class Myclass
How to get error such as Syntax Error. need ) On line 5
Thanks you
$res = exec($command_compile, $compile_result, $return_compile);
inside $res you get the last line of output of the executed command
If javac returns more lines (either error or informative messages) you may want to parse the $compile_result array that contains every output line.
foreach($compile_result as $compile_result_line) {
// do what you need with $compile_result_line
}
Anyway see http://php.net/function.exec for a detailed explanation of exec
The error you get is not from PHP code but is a "java" issue when you call javac. It seem you're trying to compile a file that reference an undefined class.

Modx Evo with PHP 5.4 and eval

Got a bit of an issue with Modx and latest version of PHP.
It's a very old version of Modx (don't ask - I have exactly the same thoughts, wasn't in my control). The site is working fine on PHP 5.2.
The problem is the web host is upgrading to PHP 5.4 - as a result the site breaks completely. The one issue I can't get a solution for is the use of eval within manager/includes/document.parser.class.inc.php under "evalSnippet()" function where it calls depreciated eval() function.
I've looked at possibliity of upgrading Modx to latest which is 1.0.9, however this still uses eval() -> even though it explicity states this version supports PHP 5.4. Below is the code:
function evalSnippet($snippet, $params) {
$etomite= $modx= & $this;
$modx->event->params= & $params; // store params inside event object
if (is_array($params)) {
extract($params, EXTR_SKIP);
}
ob_start();
$snip= eval ($snippet);
$msg= ob_get_contents();
ob_end_clean();
if ((0<$this->config['error_reporting']) && isset($php_errormsg))
{
$error_info = error_get_last();
if($error_info['type']===2048 || $error_info['type']===8192) $error_type = 2;
else $error_type = 3;
if(1<$this->config['error_reporting'] || 2<$error_type)
{
extract($error_info);
if($msg===false) $msg = 'ob_get_contents() error';
$result = $this->messageQuit('PHP Parse Error', '', true, $type, $file, 'Snippet', $text, $line, $msg);
if ($this->isBackend())
{
$this->event->alert("An error occurred while loading. Please see the event log for more information<p>{$msg}{$snip}</p>");
}
}
}
unset ($modx->event->params);
return $msg . $snip;
}
Is there away around this? Has anyone managed to get Modx Evo working with PHP 5.4?
Continued from comments on original post...
session_is_registered() is deprecated as of PHP 5.3. You'll need to check through your snippets and find out which one is using this function, then replace it with isset($_SESSION['name_of_variable']).
Quickest way to find it would be to run a %LIKE% search in phpMyAdmin for session_is_registered on the modx_site_snippets table

Why is DOMDocument not working with WAMP server?

I’m using DOMDocument to retrieve several bits of text from a webpage and place them into an array. The same code works on another server, yet doesn’t on mine. I get Trying to get property of non-object for each iteration of the while loop and the array remains empty at the end.
$html = file_get_contents("http://sugarkettle.site44.com/catering.html");
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($html);
libxml_clear_errors();
$meatPrices = array();
function fillArrayFromDOM($array,$type) {
global $doc;
$i = 0;
$label = 1;
$array = array();
while ($label <= 15):
$array[$i] = $doc->getElementById($type.$label)->textContent;
$i++;
$label++;
endwhile;
return $array;
}
fillArrayFromDOM($meatPrices,"meat");
echo var_dump($meatPrices);
Here’s a link to it working:
http://www.evintr.com/willtest.php
He’s running a GoDaddy server and I have a local WAMP (2.2) server. Any configuration options I can provide that might explain why this is happening? Or does the problem have nothing to do with my server config?
Any help much appreciated. Thanks in advance!
Update 1 - 11/16/12
On my server, I've tested $meatPrices[1] = $doc->getElementById('meat1')->textContent; and it works. For whatever reason, inside the while loop the same expression (except with variables in the getElementById parameters) tosses an error: Trying to get property of non-object.
Update 2 - 11/17/12
My WAMP server is running PHP version 5.3.13.
My friend's server is running PHP version 5.3.6.
Try with adding allow_url_fopen=on in your PHP configuration file (php.ini). Save it and restart Apache; it should work...
EDIT:
Check also if you have extension php_openssl.dll enabled (extension=php_openssl.dll in your php.ini file). Again, restart of Apache would be required.
EDIT:
It depends on PHP version you have but here are two potential solutions:
replace line fillArrayFromDOM($meatPrices,"meat"); with
$meatPrices = fillArrayFromDOM($meatPrices,"meat"); You can also
change your function to remove necessary $meatPrices parameter).
or
replace line function fillArrayFromDOM($array,$type) { with function fillArrayFromDOM(&$array,$type) { //note new character &; it will keep reference to $array variable so it could be changeable; you can also remove line: $array = array();
Both should work; I am in rush have no time to wait on your comment response. Let us know what you get...

Fatal error: Call to undefined function checkdnsrr()

My application checks MX-records on the registration page. It works fine on my local development machine (Windows 7 with WAMP Server) and on my hosting account (Linux server). Recently I deployed the app on another hosting account and I got the following error when I tried to register an user:
Fatal error: Call to undefined function checkdnsrr() in
D:\home\memorytreephoto.com\wwwroot\MyCMS\controls\register\validate_email.php
on line 27
My code is below:
<?php
// ------------------------------------------------------------
// VALIDATE E-MAIL
// ------------------------------------------------------------
if (!filter_var($txbEmail, FILTER_VALIDATE_EMAIL)) {
$emailNotValid = $email_error;
$emailvalidate_error = 1;
}
if (filter_var($txbEmail, FILTER_VALIDATE_EMAIL)) {
if (domain_exists($txbEmail)) {
$emailvalidate_error = 0;
} else {
$emailNotValid = $emailmx_error;
$emailvalidate_error = 1;
}
}
// Check if MX-records are present
function domain_exists($emailtocheck, $record = 'MX') {
list($user, $domain) = preg_split('/#/', $emailtocheck);
return checkdnsrr($domain, $record);
}
?>
Does someone know how to fix this?
A look in the manual shows that this function exists on Windows only since PHP 5.3.0. You'd have to upgrade to that PHP version to make the function work.
Alternatively, there is a PEAR Class that provides the functionality to PHP versions < 5.3.0
This function is only available in PHP 5.3.0 or higher if you're running Windows.
Maybe you need to remove the checkdnsrr from disable_functions at php.ini file or maybe your hosting provider do not have this fuction in the php.ini. Check this also, another ideas about PHP version.

Categories