Archive for April, 2011

PHP classes autoload (namespaces) Part II

Wednesday, April 20th, 2011

Ok so I made a few tests.

As a matter of fact I think it is better to use the function I wrote about in my previous post.

Turns out that I removed a couple of lines and added some more. At the end I decided to use the set_include_path because it’s more suitable for documentation purposes as well as configuration.

So after a few minutes I came up with this:

define('CLSS_PTH','path_to_classes_folder');
define('NMSPCS_PTH','path_to_namespaces_folder');
set_include_path(get_include_path() . PATH_SEPARATOR . CLSS_PTH);
set_include_path(get_include_path() . PATH_SEPARATOR . NMSPCS_PTH);

function __autoload($class){
    $class = strtolower($class);
    if(strpos($class,'\\')!==false){
        $class = preg_replace('/\\\/',DIRECTORY_SEPARATOR,$class);
    }
    $possibilities = explode(PATH_SEPARATOR, get_include_path());
    foreach($possibilities as $path){
        if(file_exists($path . DIRECTORY_SEPARATOR . $class . ".php")){
            require_once $path . DIRECTORY_SEPARATOR . $class . ".php";
        }
    }
}

So I added a path where to look for classes and namespaces, afterwards we go through all of those paths searching for the file. Works like a charm!

PHP classes autoload (namespaces)

Wednesday, April 20th, 2011

I like PHP, OOP is very easy with it and Zend Framework is a charm once you start to get use to it.

One of the coolest things in the latest release of PHP are namespaces, really they are cool! Weird as it may sound coding in PHP reminds me of Java ;P Namespaces allows me to give some order to my code and avoids overwriting other code, it’s pretty neat but one thing I’ve been meesing around is autoloading namespaced classes.

After a few hours I’ve managed to code a simple classes and namespaces autoloader, it’s the first version so I’ll be enhancing it over the next days.

I’ll try to make it as clear as possible. Code will be commented. Hope it helps someone 😀

Ok there are two kinds of files I want to autoload: namespaced classes and normal classes.

For both of them I’ll need a path:

define('CLSS_PTH','path_to_classes_folder');
define('NMSPCS_PTH','path_to_namespaces_folder');

Now to the function!

For standarization purposes class names can be camelcase but when searching the file’s name it must be lowercase.

I’ll code the body of the autoload function provided by php.

function __autoload($class){
  $class = strtolower($class);
  if(strpos('\\',$class)){
    $class = preg_replace('/\\/',DIRECTORY_SEPARATOR,$class);
    require_once (NMSPCS_PTH . DIRECTORY_SEPARATOR . $class . ".php");
  }
  else{
    require_once (CLSS_PTH . DIRECTORY_SEPARATOR . $class . ".php");
  }
}

That’s it… now I just read about a php function called set_include_path and just saw other way to autoload classes… I’ll leave that for version 2 hehehe

.htaccess maintenance

Wednesday, April 13th, 2011

Again a while since the last post but honestly I have a lot of work to do, fortunately one of the projects I’m involved requires me to learn Python.. at last!

Anyway, one division of the company hired a third party to develop a web page. The provider finished the development and finally sent everything to us. It was a php webpage, it uses the Cake Framework and MySQL so everyone thought it was going to be very easy to release the webpage in the company’s server.

A few weeks before today we decided to order the site folder and create special boxes for mini sites and small projects, even though url may seem as if the folder is directly under the web server root, it isn’t. This is cool because it keeps things from being a mess and maintenance becomes pretty easy.

So, as I was saying, we got the scripts for both the site and the database. Database was easy, we just imported the sql file and voilá! done!

The problem rised when we got to the site’s scripts. We typed the url correctly and ¡nothing! so we were surprised, everything was working fine so it wasn’t possible for a single site not to work. We read the logs, enabled the debug and nothing raised. After a few days we decided that the provider should help us. So he happily agreed to bring his people to help me…. same result. Something strange was happening, finally the provider quit and told us that the development was working on their servers and they were happy with that…. sorry :s

I got mad because of this but my boss and I decided we should dig this in. After a few days we found the cause of the problem. When we ordered the web server folders we enabled an alias to a folder. Cake uses htaccess files to provide MVC functionality. When we enabled the alias folder we forgot to enable htaccess parse, duh! so we learned that each directory that will be included on a web server should have a conf line like this:

<Directory /absolute/path/to/alias/folder>
Options -Indexes Includes FollowSymLinks MultiViews ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>

We added the proper lines, modified the htaccess rewrite rules to support 2 different domains and now everything works.

We keep learning everyday 😀

Ad Vitam Paramus