I have a problem with finally blocks and autoload. I am using PHP 5.5.9. Here is a minimal example:
<?php
function loadMyClass($class){
include_once $class . '.php';
}
spl_autoload_register("loadMyClass");
try {
try {
// Test::printIt("before "); <--- (1) Commented for now
throw new Exception();
}
finally {
echo "finally1 "
Test::printIt("finally2 ");
}
}
catch (Exception $e) {
Test::printIt("catch ");
}
Test.php:
<?php
class Test {
public static function printIt($i) { echo $i; }
}
As you see, we thrown an exception, and want a finally and a catch block to run. Test::printIt is a simple method that calls echo on its parameter. The class Test is loaded via a the registered autoload function. Running this program prints finally1 catch instead of finally1 finally2 catch, so the call to Test::printIt(1); seems to abort the finally block. The problem seems to be the autoload in the finally block. If we uncomment the line (1), then Test is already loaded in the finally block. In this case before finally1 finally2 catch is echoed correctly.
What is the problem here? Is this a bug in PHP? If yes, in which version is it fixed? Or is it my fault because autoloading is supposed to fail in finally blocks?
This is my exact PHP version as output by php -v:
PHP 5.5.9-1ubuntu4 (cli) (built: Apr 9 2014 17:11:57)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
with Zend OPcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies
This appears to be a bug in PHP, according to this bug report from April 2014 and this bug report (with links to the fix) from January 2014.
A comment from the person assigned:
As we talked, this is a knew issue, which should be fixed in 5.6+, but due to ABI break, we can not bring the fix to 5.5...
Related
I have been using the CLI interface to send out cron jobs from my codeigniter page. It worked fine until I updated Wordpress yesterday. I do not know how this effected Codeigniter but that is when the trouble started. I also installed cURL at about the same time. I am not sure if that could have made a difference.
SYMPTOMS:
None of my codeigniter CLI scripts work. I have two scripts that send out email reminders, and another that synchronizes my database and none function.
ERRORS:
I had some errors come up when I tried to run my scripts such as:
Use of undefined constant __DIR__ - assumed '__DIR__'
This was never a problem before. But for now I change that to
dirname(__FILE__)
and that seemed to help. At least that error stopped.
Next another error notice appeared regarding code in my scripts that I was not getting before: "Can't use method return value in write context in . . ."
This error was in reference to this line of code:
if (!empty($this->get_available_hours($date, $provider_id))) {
I modified this to
$availabehours=$this->get_available_hours($date, $provider_id);
if (!empty($availabehours)) {
And the error stopped. But the script usually sends out email regarding availability and no email is sent.
Now I have no errors. I run the scripts and I get no results. If I purposefully mess with the code and do things wrong, I get the appropriate error messages. So, at some level it is reading the file.
I tried just running a simple "hello world file" as discribed here
https://ellislab.com/codeigniter/user-guide/general/cli.html
And nothing was returned.
I tried a simple email script that would send out an email without accessing my database and it did not send anything to me.
It appeares to me like something has caused my code to be interpreted in an older version of php. So I looked at the version currently running:
When logged into the terminal in PuTTY I get:
PHP 5.2.17 (cli) (built: Feb 23 2012 10:42:34)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2010 Zend Technologies
Out of date ...
But when I look in the terminal within WinSCP I get:
PHP 5.5.28 (cli) (built: Sep 4 2015 12:07:49)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies
This looks up to date.
Running this works: php -r 'echo "Hello World!\n";'
MY QUESTIONS:
1) What tests can I run to find out what is blocking things with my CLI?
Any tips would be appreciated.
2) Why am I seeing two different versions of PHP depending on the terminal I am running and could this be the cause of my problem?
Hi there i followed these instructions (http://pietervogelaar.nl/php-xdebug-netbeans-vagrant/) to get XDebug up and running with vagrant and NetBeans.
It now works .... almost.
Anytime i run a code that fire a breakpoints, NetBeans works fine allowing me to do my debug thing with no problems.
The thing is at the end of the request Laravel will always return that error :
Call to a member function getAction() on a non-object, specifying a code location that looks weird : xdebug://debug-eval:1.
Even stranger, my page is rendered and the html is here, but the standard laravel error page is rendered also, at the end of my own page ......
Of course if i disable my breakpoint, everything runs just fine.
My versions right now :
Apache :
Server version: Apache/2.2.22 (Ubuntu)
Server built: Apr 17 2014 21:49:25
PHP :
PHP 5.4.28-1+deb.sury.org~precise+1 (cli) (built: May 5 2014 09:32:44)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies
with Xdebug v2.2.3, Copyright (c) 2002-2013, by Derick Rethans
Ok i finally found it.
Problem doesn't come from XDebug or Php or Laravel.
Problem comes from Netbeans.
I had an expression in my watch list i wasn't using anymore.
Deleting every custom entries in the watch list just solves the issues.
Hope at some point it helps someone else ....
helper.php : Required at the top of index.php
function __autoload($className) {
$path = ROOT_DIR.'/class/'.strtolower(trim($className)).'.class.php';
if (file_exists($path)) {
require_once($path);
}
}
function load_libs($class_variable_name) {
global $session;
global $_libs;
$_libs[$class_variable_name] = new $class_variable_name($session);
return $_libs[$class_variable_name];
}
From index.php I create an instance of a class :
new some_class_one();
No problem, it loads class file via __autoload.
But, in case that I call :
load_libs('some_class_two');
I getting error :
Fatal error: Class 'some_class_two' not found in C:\wamp\www\helper.php on line
Any idea what to do ?
I just tested this on my machine and it works perfectly. I could not replicate the issue. This is the version of PHP I'm running:
$ php -v
PHP 5.4.17 (cli) (built: Aug 25 2013 02:03:38)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
I would suggest that you remove the .class from your filenames. It's better to use namespaces and have the namespace/class name match the filesystem. Other than that, either your ROOT_DIR is incorrect or your file is located in the wrong place.
As stated here php.net/return, there is an option to stop executing include with 'return' statement.
Who can tell me why this works as stated below
// test1.php
include 'test2.php';
var_dump(class_exists('TestClass_ShouldntBeDefined'));
// test2.php
return;
class TestClass_ShouldntBeDefined { }
// run
$ php -f test1.php
bool(true)
Why this is true?
When test2.php is changed to any other form of code execute (if(true) { ... })
return;
{
class TestClass_ShouldntBeDefined { }
}
then it works as expected
$ php -f test1.php
bool(false)
PHP version
$ php -v
PHP 5.4.7 (cli) (built: Sep 13 2012 04:20:14)
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies
with Xdebug v2.2.1, Copyright (c) 2002-2012, by Derick Rethans
Return ends execution, but does not stop the include.
In the first case, the file is included, and execution stops after the return and control is given back to test1.php. But the class is included, and therefore class_exists returns true. So case 1 works as expected.
In the second case, by using the curly braces, the class definition becomes part of the execution. The return ends execution of the include, before the class is defined.
Classes and functions are not a part of sequential execution of the script. the return statement only ends the sequential execution not the oop execution.
I'm integrating my software (PHP) with SalesForce, using the SalesForce PHP Toolkit.
So far, everything's worked great, but when I started writing the code to call convertLead(), I got a "Segmentation Fault" error.
This is the code I'm running:
require_once('../salesforce/SforceEnterpriseClient.php');
ini_set('soap.wsdl_cache_enabled', 0);
$SForce = new SforceEnterpriseClient();
$result = $SForce->createConnection('../salesforce/enterprise.wsdl.xml');
$result = $SForce->login('user', 'pass+token');
echo "Logged In!";
$data = array(
'convertedStatus' => 'Converted',
'leadId' => '00QC000000mDcmJMAS'
);
$result = $SForce->convertLead(array($data));
That's it. And i'm getting a Segmentation Fault. I tried using StdClass instead of a keyed array, same thing. The convertLead method in the SF toolkit is really simple, it just calls the same method into a SoapClient instance...
NOTE: I'm running this script from the CLI, not through Apache.
UPDATE: Just tried running "strace" with my script. The last lines of it are:
close(4) = 0
write(1, "Logged IN!", 10Logged IN!) = 10
open("error_log", O_WRONLY|O_CREAT|O_APPEND, 0644) = 4
--- SIGSEGV (Segmentation fault) # 0 (0) ---
+++ killed by SIGSEGV +++
Also, in case it's relevant:
php --version
PHP 5.2.13 (cli) (built: Jul 17 2010 22:01:13)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2010 Zend Technologies
with eAccelerator v0.9.6.1, Copyright (c) 2004-2010 eAccelerator, by eAccelerator
with the ionCube PHP Loader v3.3.20, Copyright (c) 2002-2010, by ionCube Ltd., and
with Zend Optimizer v3.3.9, Copyright (c) 1998-2009, by Zend Technologies
This also happens in my dev machine (Windows), so I doubt it's the Accelerators or anything like that:
php --version
PHP 5.2.13 (cli) (built: Feb 24 2010 14:37:44)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2010 Zend Technologies
This may or may not be specific to SalesForce. Probably not, seems like a bug in the SOAP PHP library. Maybe the request/response are broken, but I can't see those because they're HTTPS.
Any ideas how I can go about diagnosing (or more importantly, working around) this issue?
Thank!
Daniel
There is a bug (still after 4 years) in the PHP soap extension. It has to do with how it handles WSDL caching. Disabling caching with ini_set() does not work. You also need to turn off caching for your particular client instance.
return new SforceEnterpriseClient('../salesforce/enterprise.wsdl.xml', array(
'cache_wsdl' => WSDL_CACHE_NONE
));
This is true even when using the native PHP SoapClient class.
Ok, this doesn't solve the underlying issue, but, in case someone got this same problem with SalesForce and PHP...
Despite what the documentation implies, fields SendNotificationEmail and OverwriteLeadSource ARE MANDATORY, you MUST specify them in the call.
Not doing so should give you a nice error, not a SegFault, but still, that solves it.