Can PHP Traits be disabled in 5.4.x? - php

I have a client using Rackspace Cloud Sites which advertise PHP 5.4 on their platform but I have been advised via their online support that traits cannot be used.
When using traits I receive a 500 error and finding no issue with the code I asked their online support to be told "it is not allowed in our environment". Using the basic PHP example code below results in a 500 Internal Server Error:
class Base {
public function sayHello() {
echo 'Hello ';
}
}
trait SayWorld {
public function sayHello() {
parent::sayHello();
echo 'World!';
}
}
class MyHelloWorld extends Base {
use SayWorld;
}
$o = new MyHelloWorld();
$o->sayHello();
Is there some reason why Traits would be disabled or can they even be disabled? The version reported by phpinfo() is 5.4.10.

After some discussions with rackspace support it seems the issue is with xcache and execution of some items such as traits. Adding the following line to .htaccess resolves the issue:
php_flag xcache.cacher 0
Seems it is not a rackspace issue but an xcache issue.

Php traits cannot be disabled. If you have a limited use of traits, you could comment out the "use" statements.

Related

PHPExcel not working php 5.2 AppServ

I am using php 5.2.6 in AppServ on a Windows machine and PHPExcel does not seem to work and produces no errors. I have error_reporting set to E_ALL.
On my Linux machine where I am using php 5.6 and using vagrant/homestead the same code works just fine.
Here is my code:
http://pastebin.com/6dJC8gaP
I added some echos to see where exactly it dies, and it seems to die on line 9. I had thought maybe it was an issue with php 5.2 and :: referencing, so I tried using the call_user_func, which also works on my php 5.6 but does not work on my php 5.2
http://php.net/manual/en/function.call-user-func.php
<?php
namespace Foobar;
class Foo {
static public function test() {
print "Hello world!\n";
}
}
call_user_func(__NAMESPACE__ .'\Foo::test'); // As of PHP 5.3.0
call_user_func(array(__NAMESPACE__ .'\Foo', 'test')); // As of PHP 5.3.0
?>
Per page:
Quote:
In PHP v5.2, you /can/ use call_user_func(array($this, 'parent::SOME_FUNCTION')).
If you don't have custom __autoload() function, you are good to go.
If you do have custom __autoload(), you need to make it `parent' aware. Something like:
Rationale: PHP 5.2 surprisingly tries to autoload a class named 'parent'. However, if you don't do anything in __autoload() for the 'parent' class, it'll work just fine.
In PHPExcel/Settings.php there are two references to libxml_disable_entity_loader function.
That function is not available until php 5.2.11, and the # in front of it was causing a silent error.
I did as they suggested and wrapped that function in function_exist checks. Everything works fine now.
Reference: https://github.com/PHPOffice/PHPExcel/issues/339

php 5.5.9 gives error when subclass is declared before parent

in my installation of php 5.5.9 on ubuntu 14.04 server, I get error when subclass is declared before parent.
testing.class.php
<?php
class mysubclass extends parentclass
{
}
class parentclass
{
}
myheader.inc.php
<?php
// empty header
index.php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once 'myheader.inc.php';
require_once 'testing.class.php';
echo "testing";
I get the following error
Fatal error: Cannot redeclare class mysubclass in /var/www/clients/client1/web3/web/central/try/php559/testing.class.php on line 4
when I replace the places of parent and subclass in testing.class.php I do not get the error.
The only possible explanation for this is that you have set up an autoloader. When PHP hits extends parentclass, it autoloads the file again to get its implementation, which also reloads mysubclass, which leads to the error.
You need to include/define/load parent classes before subclasses, there's no other way. Preferably every class should be defined in its own file as well.
after months, I found the source of the problem; it is not PHP, it is xcache. Since PHP 5.5 includes opcache, I was running both xcache and opcache on server. I uninstalled xcache and problem disappeared. I don't know which one is better, I prefer to use what comes with PHP.

CodeIgniter - Controller Double Inheritence Breaking When Deploying To Server

I really need some guidance here as this issue is pretty confusing and extremely different to understand for a beginner like myself.
I am running a WAMP server with the latest CI version.
In core/MY_Controller.php I have:
public GeneralController extend CI_Controller {
public GeneralController() {
parent::__construct();
// Does some stuff
}
}
public AuthenticatedController extend GeneralController {
public AuthenticatedController() {
parent::__construct();
if(!loggedIn()) redirect("/login");
// Does some stuff
}
}
public UnauthenticatedController extend GeneralController {
public UnauthenticatedController() {
parent::__construct();
if(loggedIn()) redirect("/home");
// Does some stuff
}
}
My login controller is:
class Login extends UnauthenticatedController {
So basically if they are logged in and load "/login" they will be routed to "/home".
This works perfectly on my local environment.
Once I upload it to my server and navigate to "/login" I get an infinite loop. After debugging I figured out that the Login controller loads AuthenticatedController instead of UnauthenticatedController so it keeps redirecting back to "/login" infinitely.
Well, now the inheritance is broken for some reason so I need to check if it calls both Auth and Unauth controllers. Nope. Just calls Auth even though it extends UnauthenticatedController.
I am at a loss here, I've tried everything I can imagine but as a new php programmer I thought I would pick some of your brains on things to try!
Thank you!
Solution:
Check your production server php version vs. local
I also had this problem with extending core classes. My problem was that is was working on my local server, but not on my production server. I was receiving the 404 error in production, and only on my controllers that used the class extensions. After some research I noticed that my local server was running php version 5.3.x, while my production server was running 5.2.x. To fix this I had to upgrade my production server to 5.3.
To find out what version of php your site is using, place this command in your view:
<?php echo phpversion() ?>

Is it possible to debug PhpUnit tests with --process-isolation option?

For unittest
class SampleTest extends PHPUnit_Framework_TestCase
{
public function testBreakpoint()
{
$a = 18;
}
}
with breakpoint on line 5 "$a = 18;",
Xdebug v2.1.0,
PHPUnit 3.6.10,
PHP 5.3.6,
ubuntu 10.11
Running unittest with NO --process-isolation option stops script execution on the line 5, as expected.
Running the same configuration WITH --process-isolation option does not stop execution on line 5.
The option --process-isolation runs every test in new process using 'proc_open' in runJob function in https://github.com/sebastianbergmann/phpunit/blob/3.6/PHPUnit/Util/PHP.php
Tested with PhpStorm 3 and vim 7 with debugger plugin. It allows to debug PHPUnit itself, but not testcases.
Is there any way to debug the child process created by PhpUnit using Xdebug? may be Zend Debugger?
As stated in the comments on the question. The issue is that PHP Storm didn't support multiple parallel debugging sessions.
Go into PHPStorm Project Settings - PHP - Debug and set Xdebug to "force break at first line when script is outside the project".
It should break on some main() method and if you step over a couple of times (or press resume), it will reach your test.
This isn't a perfect answer, but you can surround any block of code with xdebug_start_trace() and xdebug_stop_trace() calls to generate a stack trace for a targeted block of code. I've used this to see exactly what it happening at specific points in my unit tests when testing other peoples' code.
class SampleTest extends PHPUnit_Framework_TestCase
{
public function testBreakpoint()
{
xdebug_start_trace('/tmp/testBreakPointTrace');
$a = 18;
xdebug_stop_trace();
}
}
Just keep in mind that any failures will cause PHPUnit's exception handler to step in and cause the stack trace to look a little strange. If you are getting an error, you can get a clean trace by adding an exit; call right after xdebug_stop_trace:
class SampleTest extends PHPUnit_Framework_TestCase
{
public function testBreakpoint()
{
xdebug_start_trace('/tmp/testBreakPointTrace');
$a = 18;
xdebug_stop_trace();
exit;
}
}

why is Eclipse objecting to `static::$var`?

I have the following static function in a PHP class:
static function __callStatic($method,$args){
$called=NULL;
if(empty(static::$collection)) static::slurp();
if(method_exists(static::$objtype,$method)){
foreach(static::$collection as $obj){
$called[]= call_user_func_array(array($obj, $method), $args);
}
} else if (property_exists(static::$objtype,$method)){ //$method isn't a method, it's a property
foreach(static::$collection as $obj){
$called[]= $obj->$method;
}
} else if($method=='collection'){
$called=static::$collection;
} else {
throw new ZException("$method does not exist");
}
return $called;
}
The static variables are all defined but possibly not set. The code appears to do what I want it to and throws no errors of any level. But yet my new installation of Eclipse (Helios) PDT has marked every instance of static::$var as an 'unexpected static' error. If I replace static::$var with self::$var the Eclipse error goes away- but then the code doesn't work.
How do I convince Eclipse that these are not errors?
Eclipse for PHP Developers
Version: Helios Service Release 1
Build id: 20100917-0705
on 64 bit CentOS
Late Static Binding was introduced with PHP 5.3. Check Window > Preferences > PHP > Executables and Interpreter to make sure Eclipse is using PHP 5.3.
The use of static:: was introduced in PHP 5.3.
My guess would be that Eclipse is parsing according to PHP 5.2 rules. Either that, or its an oversight when they implemented the 5.3 rules in Eclipse.
Either way, you may be able to upgrade or patch Eclipse with a new rule set to get it to parse 5.3 syntax correctly.

Categories