1. install the project plugin for VIM. Here is the link: http://www.vim.org/scripts/script.php?script_id=69
2.
sudo aptitude install exuberant-ctags
cd ~
wget http://rafal.zelazko.info/wp-content/uploads/ftp/vim-custom.tgz
tar -xvzf vim-custom.tgz -C ~
Link for step 2: The link: http://rafal.zelazko.info/2010/04/18/vim-php-symfony/
Friday, March 18, 2011
Tuesday, March 15, 2011
install mongodb php driver on windows
Download the correct driver for your environment from http://github.com/mongodb/mongo-php-driver/downloads. For my case, i download "mongo-1.1.4.zip — All of the Windows build for the 1.1.4 release"
Unzip the package. There are several entries, for example:
mongo-1.1.4-php5.2vc6
mongo-1.1.4-php5.2vc6ts
...
Based on the official document, vc6 is for Apache (vc9 is for IIS). 'ts' means Thread Save. Thread safe is for running PHP as an Apache module (typical installation), non-thread safe is for CGI.
So i copy the mongo.dll from the mongo-1.1.4-php5.2vc6ts folder to my php/ext/ folder. And then update my php.ini by adding extension=php_mongo.dll
Finally, restart my apache and then have a look at phpinfo(), i can find a mongo section. It is done
Unzip the package. There are several entries, for example:
mongo-1.1.4-php5.2vc6
mongo-1.1.4-php5.2vc6ts
...
Based on the official document, vc6 is for Apache (vc9 is for IIS). 'ts' means Thread Save. Thread safe is for running PHP as an Apache module (typical installation), non-thread safe is for CGI.
So i copy the mongo.dll from the mongo-1.1.4-php5.2vc6ts folder to my php/ext/ folder. And then update my php.ini by adding extension=php_mongo.dll
Finally, restart my apache and then have a look at phpinfo(), i can find a mongo section. It is done
Monday, March 14, 2011
PHP case insensitive file_exists
We know that PHP's file_exists is case sensitive on *nix system. However, i've been through a situation that i need this function case insensitive. For example, there exists a file: /var/www/project/public/images/company/logo.png. In Url it will be http://project.com/images/company/logo.png. Now, if the html code is < img src = "http://project.com/images/Company/logo.PNG" > or even < img src = "images/Company/Logo.png" >, it works fine on windows. But on linux, it won't work. My task is, i need to allow users to export some special html pages in a zip archive so that they can browse them offline. If the html pages contain image links, the images will be exported too if they exists. Well, as you can imagine, it is pretty much like the "Save as..." function when you right click your mouse on a web page.
I can't find a perfect solution. Some solutions only allow you to have file name case insensitive, and you still have to ensure the directory name of the file must be correct in case sensitive way. Some solutions use customized glob() functions that i can't easily understand. So i finally decided to implement my own solution based on my situation.
Here is my thought: some specific path is defined in your config file and you can never get it wrong. For example, you may have config settings like this:
//paths
$conf['root_dir'] = dirname(__FILE__) . '/..';
$conf['public'] = $conf['root_dir'] . '/public';
$conf['images'] = $conf['public'] . '/images';
So, the only uncertain part is the sub folders and files under the 'images' folder. Here is how i implement my case insensitive file_exists():
//$filename is the full path file name, like /var/www/project/public/images/Company/Logo.PNG
//$baseDir is the base directory that you can't get it wrong, here it might be /var/www/project/public/images/
//return: the function return false if file NOT exists,
//it will return the correct case sensitive path if the file exists in case insensitive way.
How is this function's performance if we have huge number of files and folders under $baseDir? I haven't really tested this issue yet. At the moment this function serves my purpose very well. However, i'm looking forward to any other decent implementation.
I can't find a perfect solution. Some solutions only allow you to have file name case insensitive, and you still have to ensure the directory name of the file must be correct in case sensitive way. Some solutions use customized glob() functions that i can't easily understand. So i finally decided to implement my own solution based on my situation.
Here is my thought: some specific path is defined in your config file and you can never get it wrong. For example, you may have config settings like this:
//paths
$conf['root_dir'] = dirname(__FILE__) . '/..';
$conf['public'] = $conf['root_dir'] . '/public';
$conf['images'] = $conf['public'] . '/images';
So, the only uncertain part is the sub folders and files under the 'images' folder. Here is how i implement my case insensitive file_exists():
//$filename is the full path file name, like /var/www/project/public/images/Company/Logo.PNG
//$baseDir is the base directory that you can't get it wrong, here it might be /var/www/project/public/images/
//return: the function return false if file NOT exists,
//it will return the correct case sensitive path if the file exists in case insensitive way.
function caseInsensitiveFileExists($filename, $baseDir)
{
//well if file_exists() can find the $filename, we can return
if(file_exists($filename) {
return $filename;
}
//we only do this on *nix system, on windows, we don't have to worry about case sensitive issue.
if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') {
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($baseDir));
foreach ($iterator as $value) {
if ($value->isFile()) {
$filePath = $value->getPathname();
//if they are equals, we know the $filename exists.
if (strotolower(filePath) == strtolower($filename)) {
return filePath;
}
}
}
}
return false;
}
How is this function's performance if we have huge number of files and folders under $baseDir? I haven't really tested this issue yet. At the moment this function serves my purpose very well. However, i'm looking forward to any other decent implementation.
Thursday, March 10, 2011
a difference i didn't expect between php 5.2 & php 5.3 (overriding abstract function)
Here is my code:
If i run this code on php 5.2.14 + windows, i will get "Fatal error: Declaration of news::buildModel() must be compatible with that of App_Daos_Abstract::buildModel()".
However, if i run the code on php 5.3 + ubuntu, everything is fine.
I don't think windows or ubuntu matter. I think this is a difference between PHP 5.2 and PHP 5.3. So even at this point PHP 5.3 is more closer to JAVA.
abstract class App_Daos_Abstract
{
abstract public function buildModel($result);
}
class UserDao extends App_Daos_Abstract
{
public function buildModel($result = null)
{
}
}
If i run this code on php 5.2.14 + windows, i will get "Fatal error: Declaration of news::buildModel() must be compatible with that of App_Daos_Abstract::buildModel()".
However, if i run the code on php 5.3 + ubuntu, everything is fine.
I don't think windows or ubuntu matter. I think this is a difference between PHP 5.2 and PHP 5.3. So even at this point PHP 5.3 is more closer to JAVA.
Wednesday, February 23, 2011
DAO vs ActiveRecord
There are a lot of discussions about DAO/DataMapper & ActiveRecord. Here are some articles:
http://codeutopia.net/blog/2009/01/05/decoupling-models-from-the-database-data-access-object-pattern-in-php/
http://weierophinney.net/matthew/archives/202-Model-Infrastructure.html
http://codeutopia.net/blog/2009/01/05/decoupling-models-from-the-database-data-access-object-pattern-in-php/
http://weierophinney.net/matthew/archives/202-Model-Infrastructure.html
static methods VS singleton
Well this is an old topic but i've never seriously thought about it. We can grab a lot articles from internet, but here is what i agree most:
1. singleton is an instance, instance is an object. so, you can pass it as a parameter. If you were using static classes, the other function would have to use the class statically too and if you ever needed to change the behavior, you would need to rewrite parts of that function too instead of just having the first one pass a different class as the parameter.
2. You can also give a singleton some parameters. When working with static classes, you may need to pass a lot of parameters to the methods of the class because you aren't "constructing" it
3. singleton can become multiton(read db connection, write db connection)
4. If you use the reference in your class and decide that you won't need a singleton anymore, but a normal class, you will just need to change the behavior of the class itself and perhaps replace the line which gets an instance of it to constructing a new one or such. If you were using a static class, you would have to rewrite a lot of the code to use the new reference to the class instead of the static class name. A singleton class should also work almost out of the box as a normal class too. If you had a static class and wanted to convert it to a normal one, you would have to rewrite many parts of it.
6. You can't extend static classes, but singletons you can. Except it doesn't work very well in PHP due to some issues with static method inheritance, but it's not the only language in the world and it can be done but slightly hackily
7. Last and least:D dude, you can find many singleton in Zend Framework. Could you find any static class there?
So to the end, where is the place for static methods? Well, they definitely have their places too:
For example for library code they can be nice, such as the static System.Math class in C#
1. singleton is an instance, instance is an object. so, you can pass it as a parameter. If you were using static classes, the other function would have to use the class statically too and if you ever needed to change the behavior, you would need to rewrite parts of that function too instead of just having the first one pass a different class as the parameter.
2. You can also give a singleton some parameters. When working with static classes, you may need to pass a lot of parameters to the methods of the class because you aren't "constructing" it
3. singleton can become multiton(read db connection, write db connection)
4. If you use the reference in your class and decide that you won't need a singleton anymore, but a normal class, you will just need to change the behavior of the class itself and perhaps replace the line which gets an instance of it to constructing a new one or such. If you were using a static class, you would have to rewrite a lot of the code to use the new reference to the class instead of the static class name. A singleton class should also work almost out of the box as a normal class too. If you had a static class and wanted to convert it to a normal one, you would have to rewrite many parts of it.
6. You can't extend static classes, but singletons you can. Except it doesn't work very well in PHP due to some issues with static method inheritance, but it's not the only language in the world and it can be done but slightly hackily
7. Last and least:D dude, you can find many singleton in Zend Framework. Could you find any static class there?
So to the end, where is the place for static methods? Well, they definitely have their places too:
For example for library code they can be nice, such as the static System.Math class in C#
Unset problem with Collection implements Countable, Iterator, ArrayAccess
I just have one problem with my Collection class implements Countable, Iterator, ArrayAccess. Let's look at the code below:
foreach ($Collection as $index => $model) {
//do something
unset($Collection[$index]);
}
But, obviously, I haven't found any solution to make the above code work properly. It is all about the array internal pointer, the valid() method and the offsetUnset() method. I tried different with my own implementation and with others' code(code searched from google, and code implemented by some open source software like tomatocms)
foreach ($Collection as $index => $model) {
//do something
unset($Collection[$index]);
}
But, obviously, I haven't found any solution to make the above code work properly. It is all about the array internal pointer, the valid() method and the offsetUnset() method. I tried different with my own implementation and with others' code(code searched from google, and code implemented by some open source software like tomatocms)
Subscribe to:
Posts (Atom)