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?
Namespaces are not supported prior to 5.3. There isn't really a way to adapt for them into 5.2 unfortunately.
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.
Namespaces are available in PHP as of
PHP 5.3.0.
Source: http://www.php.net/manual/en/language.namespaces.rationale.php
http://www.php.net/manual/en/language.namespaces.rationale.php
Namespaces are available in PHP as of PHP 5.3.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