12

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?

kravemir
  • 9,380
  • 15
  • 54
  • 99
  • No. Read this cute manual: http://php.net/manual/en/language.namespaces.php – OZ_ Jun 24 '11 at 20:00
  • While not supported, you can of course remove them. As it happens that came up recently: http://stackoverflow.com/questions/1836387/strategy-for-developing-namespaced-and-non-namespaced-versions-of-same-php-code/3661724#3661724 - Not sure if you asked for that, or if this really was just about syntax highlighting? – mario Jun 24 '11 at 20:02
  • 1
    PHP preprocessor sounds good. But i also use variable class names that aren't procesed with that script. – kravemir Jun 24 '11 at 20:13

5 Answers5

28

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

Michael Berkowski
  • 253,311
  • 39
  • 421
  • 371
8

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.

DeaconDesperado
  • 8,971
  • 6
  • 43
  • 75
3

Namespaces are available in PHP as of PHP 5.3.0.

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

tplaner
  • 7,975
  • 3
  • 27
  • 46
1

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

Namespaces are available in PHP as of PHP 5.3.0.

André Puel
  • 7,716
  • 7
  • 45
  • 78
0

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

Paul 501
  • 537
  • 5
  • 12