12
votes

I've done little web using namespaces. I have it in my computer and i'm about to moving it to free hosting that uses php 5.2. Syntax highlighter for php 5.2 interprets them as errors.

Are namespaces supported by php 5.2?

If not is there any way how to use them with little changes to existing code?

5
No. Read this cute manual: php.net/manual/en/language.namespaces.phpOZ_
While not supported, you can of course remove them. As it happens that came up recently: stackoverflow.com/questions/1836387/… - Not sure if you asked for that, or if this really was just about syntax highlighting?mario
PHP preprocessor sounds good. But i also use variable class names that aren't procesed with that script.kravemir

5 Answers

28
votes

Namespaces are not supported prior to 5.3. There isn't really a way to adapt for them into 5.2 unfortunately.

8
votes

Namespaces are only available as of 5.3

At least in the case of classes, you can use the class_exists function to check if a class has already been defined with a like name within the global namespace. Coupled with the __autoload() function, you can create one universal alias and have the system check for both classes named by the original name as well as the name with some kind of extra identifier prepended. I'll use "ns" as an example.

function __autoload($class){
  try{
     require_once('ns'.$class.'.php');
  }catch(Exception $e){
     echo 'The class is unavailable in pseudo-namespace as well as global';
  }
}

Just make sure the require path points to wherever you keep your models. You could use a different folder instead of the alias as well.

This way, any duplicate classes can be put into files separate from the main execution that are only included if they don't exists in the global. Though this doesn't strictly fix the problem of having to physically rename the classes, it will allow you to put your definitions in different directories for versioning purposes etc.

3
votes

Namespaces are available in PHP as of PHP 5.3.0.

Source: http://www.php.net/manual/en/language.namespaces.rationale.php

0
votes

Ive just come across this problem, ive developed an image upload script myself and added some third party code to aid image processing (cropping) but they use namespaces, works fine on my develoment machine, but when i uploaded to the live server i get a Parse error.

Luckily my host supports php 5.3 and 5.4, so ive asked them to change it to 5.3 for me, im hoping that will solve the problems im having, simply removing the namespaces made the script fail :(

Paul