Parser error on a cron job

software development

#1

Good morning!
Today I noticed a funny behavior on the execution of cron jobs.

I tried to dynamically get a class constant, which worked perfectly well on HTTP request, but resulted in a parser error during a cron job.
Take a look in the code below.
The code in question is $this::NOTIFICATION_IDENTIFIER

[php]
if (version_compare(PHP_VERSION, ‘5.3.0’) >= 0) {
$id = $this::NOTIFICATION_IDENTIFIER;
// $session->addSuccess(PHP_VERSION . " $id");
} else {
$id = constant(get_class($this) . “::NOTIFICATION_IDENTIFIER”);
// $session->addError(PHP_VERSION . " $id");
}
[/php]

The code above results in this:
[php]Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM in /home///magento/app/code/local/GateOn/Core/Helper/Abstract.php on line 111[/php]

Since this is a parser error, I think the code doesn’t begin to get executed, making my version_compare statement useless…
So I had to comment the if/else statement, and be forced to use just the “else” part…

Not satisfied, I created a copy of the file, with the lines uncommented, and tried to make PHP to execute just that file using the terminal.
Note there is no code being executed, PHP will just try to create a class.
The error happens before the code execution.

This is the result when I try to execute the file with the initial $this::NOTIFICATION_IDENTIFIER
[php]bash-4.1$ php -f /home///magento/app/code/local/GateOn/Core/Helper/Abstract_1.php

Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM in /home///magento/app/code/local/GateOn/Core/Helper/Abstract_1.php on line 111[/php]

If the parsing were OK, we would see something like this:
[php]bash-4.1$ php -f /home///magento/app/code/local/GateOn/Core/Helper/Abstract.php
Fatal error: Class ‘Mage_Core_Helper_Abstract’ not found in /home///magento/app/code/local/GateOn/Core/Helper/Abstract.php on line 2[/php]

Keep in mind that the “Abstract_1.php” would work perfectly on a HTTP request! This file would be loaded in almost every request, and I would even see my session success notification message!

I wonder why the PHP parser results in an error during a cron job, but not on a HTTP request.

I believe in a server issue, but dreamhost support made me ask it here…

Thanks!


#2

It sounds as though you are using some PHP 5.3 features in this script. If so, you will need to specify that you need PHP 5.3 in the cron job (or on the command line):

The default “php” interpreter is currently PHP 5.2 for compatibility reasons. (This is independent of any setting you may have for the PHP version on your domain.) Since you are using some PHP 5.3 syntax in your job, the PHP 5.2 interpreter is unable to understand what is going on in the file, and gives up before it even starts running the script.


#3

You nailed it!
I have asked Dreamhost support with all examples above, and they said the problem was my code…

I knew it had something to do with the PHP server!

Thanks Andrew, you rock!