I have a local Drupal site running with MAMP on Win 7 with php 5.6.13. My live site has php 5.6.17. I have no problems on the local site, but on the live site I get unexpected T function error with the following code which I downloaded from drupal.org.
function redhen_activity_message_types() {
$message_types = array_keys(message_type_load());
$filtered = array_filter($message_types, function($var) {
return strpos($var, 'redhen_') !== FALSE;
});
return $filtered;
}
The line beginning with $filtered is the one that is pointed to by the error message.
I know little about php, but have been searching the web and found something called anonymous functions not working on older versions of php. Is the slightly different versions of php causing this problem? How do I fix it?
Thanks!
The anonymous function is the function that's defined starting in that line of code:
function($var) {
return strpos($var, 'redhen_') !== FALSE;
}
I don't see anything obviously wrong with that function, and Redhen CRM gets enough use that others are likely to have run into problems already if it were clearly incorrect. And while it's always good to test under the same version as your production environment it's unlikely that there was bug introduced to PHP between 5.6.13 and 5.6.17 that's causing this problem.
To find the root cause try a few things:
Check to make sure the version of PHP you think is running in production is actually running. I've seen several environments recently where people had a different version running under php-cli than on the web server, meaning that running php --version on the command line gave the wrong answer. Use a phpinfo() call to verify the version.
Check the Redhen issue queue for other people with the same problem.
Update your test machine to match production. Once you're sure production is the version you think it is, set your dev environment to match and see if the problem comes back. If it does, try to move forward to a more current minor version and see if the error goes away again.
Related
Before you continue to read - the issue has been fixed
Well... this is kinda weird.
I'm working on a web app for some friends. I have a working version uploaded to their hosting, and, suddenly, it stopped working a couple of days ago.
I haven't added anything or changed anything on the server. There is no error, simply the app stopped loading and a blank html page is loaded instead.
I've traced the problem until /vendor/composer/autoload_real.php file.
In the end of the static class inside this file, there is a loop where several other files are being included (actually requested), I've checked that when it tries to request /vendor/laravel/framework/src/Illuminate/Foundation/helpers.php, the system stops. Obviously, I've checked that the file is there, and it isn't been touched.
Well, I'm positive I've didn't changed anything on this part of the app (it's core framework, and I usually don't mess up inside), but just of a sudden it stopped working.
The working copy on my computer just works fine. I've uploaded my copy of helpers.php, but nothing changed.
Anyone had experienced similar issues recently? Anyone has any idea about how to fix it?
EDIT: It's been several days since I could check on this for the last time.
Now I've been tracing raw php execution on /vendor/laravel/framework/src/Illuminate/Foundation/helpers.php file. This is, I've started echoing messages and trying to execute just this file, to see where code execution is stopped.
I know this is very crappy debugging, but I haven't access to apache nor I can restart it, and it seems there is no easy way to get an error code without this.
So patiently trying I've reached two points where execution stops on this file:
Creation of factory method
if (! function_exists('factory')) {
/**
* Create a model factory builder for a given class, name, and amount.
*
* #param dynamic class|class,name|class,amount|class,name,amount
* #return \Illuminate\Database\Eloquent\FactoryBuilder
*/
function factory()
{
$factory = app(EloquentFactory::class);
$arguments = func_get_args();
if (isset($arguments[1]) && is_string($arguments[1])) {
return $factory->of($arguments[0], $arguments[1])->times($arguments[2] ?? null);
} elseif (isset($arguments[1])) {
return $factory->of($arguments[0])->times($arguments[1]);
}
return $factory->of($arguments[0]);
}
}
and creation of mix method
if (! function_exists('mix')) {
/**
* Get the path to a versioned Mix file.
*
* #param string $path
* #param string $manifestDirectory
* #return \Illuminate\Support\HtmlString|string
*
* #throws \Exception
*/
function mix($path, $manifestDirectory = '')
{
return app(Mix::class)(...func_get_args());
}
}
In both cases func_get_args is involved... I haven't a specific error, but I have the tingling that the problem is related somewhat to the fact that this function isn't returning anything.
EDIT2: Ok, I tried php artisan and composer commands suggested on answers to no success. The issue is still there.
I've also deleted the logs and checked that no new log was created while trying to load the site.
I've tried a clean laravel install from scriptaculous on a new directory (I've done this when I first uploaded the site, it worked perfectly for several months) and this new install doesn't reach the front page neither. A blank page (on Firefox) or a 500 error (on Chrome) is shown instead.
I forgot to mention an important data. My app is sharing space with a wordpress instance. The wordpress is installed on public_html, and my site on public_html/mySiteDirectory. This hasn't supposed any problem so far more than make me tweak lightly .htaccess file to make routing work fine.
There is also a phpbb forum sharing space on it's own directory inside public_html.
I wonder if any of those could have somewhat been upgraded and produced this strange outcome.
Also... laravel app_debug is enabled on .env file since the beginning, but never showed anything. This seems to be something that fails before laravel framework is fully loaded.
EDIT 3: IT'S FIXED.
Well... first of all, let me thank you all for your help. Actually there wasn't one answer that fixed the problem, but many of you suggested to use phpInfo() to check php version which ultimately led to the true problem.
Yes... surprisingly, although php -v on terminal showed php 7.2 running on server, and cpanel php management tools showed also php 7.2 installed and running, when I used phpInfo(); php 5.6 was showed on the response.
I used the own cpanel tools to make an upgrade/downgrade to 7.3 and again to 7.2 and et voilá phpInfo(); shows 7.2 and the page is again up and running.
I'll try to share the reward between several answers (don't know if something like that is even possible), and will +1 every answer suggesting phpInfo(); as it showed as the key to fixing this problem.
If I cannot share the reward I'll accept as final answer the one from Don't Panic, as it was the one that convinced me that there was a problem with php versions.
If you look at the code in the functions you have traced the problem to, both contain interesting or less-common PHP features:
factory() includes ??, the null coalescing operator. This was introduced in PHP 7.0.
I see in a comment you added to another answer that you are using PHP 7.2. This question describes Laravel failing in factory(), because even after upgrading to PHP 7.2, and phpinfo() showing 7.2, Apache was still using PHP 5.6. This seems unlikely, but when all else fails ... ?
mix() includes ..., the spread or splat operator. This was introduced in PHP 5.6, but there are reports of it failing in Laravel with 7.x due (AFAICT) due to mismatches between the PHP versions Apache and the CLI are using.
Considering you mentioned you uploaded the code from your local machine (rather than using php composer install), and that CPanel tends to update PHP automatically and silently, this seems like a possibility - there are 4 PHP versions in play (CLI/web on your machine, and CLI/web on server), and they all need to be in sync.
As a side note, as you've already discovered you are really working blind without the logs. Either the PHP and/or Apache logs will give you more info, typically describing exactly the problem, and save you having to manually trace things as you've been doing. If those logs aren't showing anything, maybe PHP isn't configured to log errors - try enabling that.
It probably depends on your CPanel version and config, but standard locations for your logfiles are typically:
Apache access log: /usr/local/apache/domlogs/<your-username>/
Apache error log: /usr/local/apache/logs/error_log
PHP error log: /home/<your-username>/public_html/error_log
Start wtih composer dumpautoload. Then run php artisan optimize:clear. Re-upload the whole site and you should be good to go. I have experienced a very similar issue. Do you run a pipeline on the server or are you just FTP-ing the site up? Are you able to run composer on the server and the same for artisan? If you can, you need to those commands there are as your cache is often based on your host computer drives and will fail in prod.
Simply replacing the file might not be helpful. Try enabling the error log or check if you are able to reach this file bootstrap/app.php. If yes it means there is no problem in the composer. Try checking the version of PHP installed and if it is compatible with your Laravel. Try checking PHP Error Log. It might be helpful.
Ok I should mention a few things
1: Go to storage/logs directory and delete all *.log files then you refresh the web page and you'll check out to see for any log file if there is no log file it means it's related to server configuration and etc. if there is a log file you read it and you post it in here
2: Did you pull the code on cpanel by console command using version control system like git or svn or you just uploaded it in a classic way, if you indeed pulled it with VCS you may did a composer update instead of composer install which will updates all packages as possible as defined in composer.json file and you may have got a higher version of vendor than your local and something may broke in there.
3: You may have a different php version on server vs your local which many times casing real problems.
I can't tell for sure until you post your log file here
So here is what i would have done.
If it is possible simply turn0on debugging, this will show you the error in the web browser itself (you can do that from .env file)
You can also read the laravel log file for errors as mentioned earlier.
If you do not want to do this, run the following commands
PHP artisan cache:clear
PHP artisan route:cache
PHP artisan config:cache
PHP artisan clear-compiled
PHP artisan optimize
If you could share more details that would help...
Have you checked permissions and ownership for storage and bootstrap/cache folder?
these permissions need to be set for the app to work
sudo chgrp -R www-data storage bootstrap/cache
sudo chmod -R ug+rwx storage bootstrap/cache
I tried everything and nothing worked. The best solution for me was that I deleted cpanel files and reuploaded all the codes.
Note: This is my first Symfony project, so obvious mistakes are possible.
I have just pushed my Symfony application from my local environment (where it works nicely) to a live server. Sadly, after running server:start, if I try and curl one of the local routes in the same way I tested on my local, I get the below error back:
FatalErrorException in Core.php line 1595: Parse Error: syntax error,
unexpected '.'
Note that the Core.php file is /var/www/symfony_playground/vendor/twig/twig/lib/Twig/Extension/Core.php
I didn't some searching online, and cannot see anyone else getting similar issues, so wonder whether I have a dependency issue (although I checked this).
The line in twig that is causing the issue looks like:
$ret = $object->$method(...$arguments);
Any suggestions much appreciated.
My guess is your server is not very up-to-date when it comes to PHP. Variadic functions (the ...$arguments) were introduced in PHP 5.6 so my guess is your server is running an older version than that.
Give it a debug with by echoing phpversion() or even the more detailed phpinfo().
If this is the case the only solution would be to update PHP as downgrading symfony doesn't really work.
Well, I'm transferring a little web server from one computer to other, on the 'other' computer I cannot a more updated version of PHP,
I just have to add my files through ssh and that's all, when I load my webpage it says explicitly:
The error is showing on a Debian with PHP 5.4.27 squeeze, the webserver comes from an Ubuntu with PHP 5.4.16 oneiric
Parse error: syntax error, unexpected '[' in ~directory~ on line 38
This is the line 38.
$VALUE=$DB->query("a nice sentence")->fetch()[0];
So, i was pretty surprised because PDO was accepted since PHP 5.1, so, i look deeper and i found the possible error, you can't simply do ->fetch()[0], you must separate it in order to be "processed",
However, I have transport this server with this information before, so i really don't know if it's something on the configuration of PHP or maybe the version by itself 5.4.X? You guys know any hint? As I said, i can't just install other updated version of PHP5, what do you recommend?
Have a nice night.
Edit: updated to the issue on the PHP 5.4 now
The error seems triggered by the array dereferencing. This was added in PHP 5.4.. Not sure how you made that work in 5.3.6
http://nl3.php.net/language.types.array.php
To workaround you could try
$VALUE = $DB->query("a nice sentence")->fetch();
echo $VALUE[0];
PHP Version on my ubuntu (where it works):
PHP Version 5.4.16
PHP Version on the Debian (where it does not work):
PHP Version 5.4.27-1~dotdeb.0
This version number was extracted by using phpinfo(); on the index page.
Apparently, the error was solved by JUST changing the first issue line (the other hundreds of codes that had that wasn't necessary to change, what i believe to be a little weird).
$VALUE = $DB->query("a nice sentence")->fetch()->offset(0);
I am trying to access an element of an array in PHP using the following method:
$test = $this->Student->find('first',array('conditions',array('Student.student_table_id'=>1)))['Student']['student_id'];
It seems to work well on a slightly newer version of XAMPP, but when I try to use that syntax on another machine with an older version of XAMPP for Mac OS X 1.7.3 installation, it throws me to a Server not found page. I don't know if it something to do with my XAMPP configuration or something else..
Also, this behavior, i.e. redirection to a server not found page, happens whenever there is a syntax error.
Mind you, both machines support the minimum requirements of CakePHP.
From the comment..
the PHP Version on the problematic machine is 5.3.1
That is because you are trying to use a new feature of PHP 5.4 , called the function array dereferencing.
How to fix ?
Break down your code like this.
$test = $this->Student->find('first',array('conditions',array('Student.student_table_id'=>1)));
$test1 = $test['Student']['student_id'];
The code:
<?php
class My_Test{
public $exists = 'yah';
public function test(){
return $this->exists;
}
}
$test = new My_Test;
echo $test->test();
produces the following error intermittently (every other page request on average) on two servers:
( ! ) Notice: Trying to get property of non-object in test.php on line 8
Call Stack
# Time Memory Function Location
1 0.0003 636392 {main}( ) ../test.php:0
2 0.0003 636840 My_Test->test( ) ../test.php:14
where line 10 is `return $this->exists;
Please note this is not an untested example, it is the full code that produces the error. I'm aware the code is valid but it doesn't work in two environments.
I'm really not sure why! One server is running PHP 5.3.3 (fedora), the other PHP 5.3.2 (ubuntu). I've tried rebooting the servers too. They don't share anything, although they are on the same network.
Anyone got any tips for debugging?
There's nothing wrong with your test code. It runs without issue on PHP 5.3.3. You've probably lost the cause of the issue when converting your "real" code into test code to post on here.
As people before stated the code looks fine and should not cause any errors.
Therefore I guess it is related to an error of the php engine itself:
How did you install PHP?
Did you compile the sourcecode yourself? Have ./configure or make logged any warnings or error messages while executing?
Have you installed PHP by use of a package manager? (I guess everything should be fine then).
How did you configure PHP?
Have you made any changes to php.ini like changing memory limit, paths, etc. ?
Have you installed any extension that might cause issues? My experience says often opcode caches (e.g. APC) can cause errors that cannot be easily explained. Also debuggers or other extensions may cause interferences.
Do you have any (additional) error messages in your HTTP-Daemon-Log?
Sometimes additional errors can be found in the error.log file in case the server is configured to do so (e.g. unexpected termination of script or similar).
The code appears to work as is, but I've never seen an object instantiated without the parens.
$test = new My_Test;
// Should be
$test = new My_Test();
You could also implement some logging:
public function test()
{
// Can edit this to use instanceof or getclass for further testing
if (!is_object($this)) {
error_log(serialize($this));
}
return $this->exists;
}