Sunday, April 4, 2010
DON'T USE 'CREATE TABLE XXX SELECT * FROM YYY' TO BACKUP AN INNODB TABLE
The reason is simple: the backup table will lose all key/index attributes from the original innodb table.
Monday, March 22, 2010
Replace built-in PHP fatal errors with exceptions
We may want to use Exception instead of php4 style error handling systems to make error handling more consistent. We can build a bridge from old error handling to the new Exception handling.
class PhpErrorException extends Exception {}
function errorHandler($errno, $errstr, $errfile, $errline) {
throw new PhpErrorException ($errstr,$errno);
}
$handler= set_error_handler('errorHandler');
Now you can test it. For example: run echo 5/0; and you will see an exception is thrown instead that a PHP warning is given off.
class PhpErrorException extends Exception {}
function errorHandler($errno, $errstr, $errfile, $errline) {
throw new PhpErrorException ($errstr,$errno);
}
$handler= set_error_handler('errorHandler');
Now you can test it. For example: run echo 5/0; and you will see an exception is thrown instead that a PHP warning is given off.
Friday, March 19, 2010
memory usage in PHP
A very excellent blog about PHP memory usage: http://blog.preinheimer.com/index.php?/archives/354-Memory-usage-in-PHP.html
Unlike C/C++ developers, most PHP developers never think about memory management in PHP. It is true that PHP can free the memory after a request is processed. However, if you use PHP to process critical tasks with large amount of data, you'd better read this blog carefully, and test its memory-usage-example.php by yourself. You will see how different it is.
Unlike C/C++ developers, most PHP developers never think about memory management in PHP. It is true that PHP can free the memory after a request is processed. However, if you use PHP to process critical tasks with large amount of data, you'd better read this blog carefully, and test its memory-usage-example.php by yourself. You will see how different it is.
Monday, March 8, 2010
新版(浙版)西游记
张纪中拍的. 因为勾起了对儿时西游记的怀念, 就买了下来. 正如网上评价的, 不愧是2010年第一部雷人剧集.
白骨精是孙悟空的结拜兄妹, 最后为爱自尽.
最让我羡慕的是那个如来佛祖啊,座下美女菩萨如云: 观音, 文殊, 普贤, 全是美女. 看了真让人有恨不得早日成佛的感觉.
呵呵, 总的来说, 这部西游记作为无聊时的消遣也是可以的. 但是要和六小龄童演的西游记比起来, 考虑到时代和技术的进步, 实在是看不出新版的西游记有什么突破的地方.
哦, 对了, 剧中大牌明星云集: 唐国强是玉皇大帝, 陈冲是观音, 总而言之, 本剧可作消遣和笑料, 但成不了经典.
白骨精是孙悟空的结拜兄妹, 最后为爱自尽.
最让我羡慕的是那个如来佛祖啊,座下美女菩萨如云: 观音, 文殊, 普贤, 全是美女. 看了真让人有恨不得早日成佛的感觉.
呵呵, 总的来说, 这部西游记作为无聊时的消遣也是可以的. 但是要和六小龄童演的西游记比起来, 考虑到时代和技术的进步, 实在是看不出新版的西游记有什么突破的地方.
哦, 对了, 剧中大牌明星云集: 唐国强是玉皇大帝, 陈冲是观音, 总而言之, 本剧可作消遣和笑料, 但成不了经典.
Tuesday, March 2, 2010
Interesting issue of Zend_Request Object
See the code below:
class TestController extends Zend_Controller_Action
{
public function testAction()
{
$params = $this->getRequest()->getParams();
}
}
It tries to get all parameters of a request. In most situations, it works fine. However, if the request contains a parameter like '?action=subscribe', and you try to get $action = $params['action']. Now, what do you think is the value of $action? If you say, it must be 'subscribe', then you are wrong.
How come this happen? The answer is, Zend_Request object set 3 default parameters: 'module', 'controller' and 'action', and they will overwrite the identified variables in the request. Back to the code above, if you var_dump($params), you probably will see the result like:
array(3) {
['module'] => string(7) "default"
['controller'] => string(5) "test"
['action'] => string(5) "test"
}
As you can see, the value of $action will be 'test'. What if we try to do $action = $this->getRequest()->getParam('action')? This doesn't help either, the value of $action is still 'test' instead of 'subscribe'. Same applies to $this->_getAllParams() and $this->_getParam().
So how do we get the correct value of action in the request? The answer is, for a POST request, we must exactly use $this->getRequest()->getPost('action'); for a GET request, we must use $this->getRequest()->getQuery('action')
class TestController extends Zend_Controller_Action
{
public function testAction()
{
$params = $this->getRequest()->getParams();
}
}
It tries to get all parameters of a request. In most situations, it works fine. However, if the request contains a parameter like '?action=subscribe', and you try to get $action = $params['action']. Now, what do you think is the value of $action? If you say, it must be 'subscribe', then you are wrong.
How come this happen? The answer is, Zend_Request object set 3 default parameters: 'module', 'controller' and 'action', and they will overwrite the identified variables in the request. Back to the code above, if you var_dump($params), you probably will see the result like:
array(3) {
['module'] => string(7) "default"
['controller'] => string(5) "test"
['action'] => string(5) "test"
}
As you can see, the value of $action will be 'test'. What if we try to do $action = $this->getRequest()->getParam('action')? This doesn't help either, the value of $action is still 'test' instead of 'subscribe'. Same applies to $this->_getAllParams() and $this->_getParam().
So how do we get the correct value of action in the request? The answer is, for a POST request, we must exactly use $this->getRequest()->getPost('action'); for a GET request, we must use $this->getRequest()->getQuery('action')
Wednesday, February 24, 2010
GIT as version control system
I have been using GIT as version control system for a while. What impressed me most is GIT's branch management and merging.
You have a clean code base called master. Now you need to implement a new feature to your application. What you need to do is to create a new branch for this task, just like this: git branch new_feature_1. And then you can checkout this branch: git checkout new_feature_1. Now you have switch from master to new_feature_1, and you can start work on your feature on this branch.
If one day, while you are still in the middle of the new_feature_1, your manager comes and tells you 'hey, we have an urgent feature that must be done asap, and it has the highest priority'. You can simple save all your current work: git commit -a. This will commit all your work to the new_feature_1 branch. Then you can checkout master, the clean code base and create a new branch: urgent_feature.
Most people using CVS/SVN have painful experience with branch merging. GIT makes merging an incredibly simple and easy task. After you finish your work, you can simply merge it from master: git merge urgent_feature. Now, you can checkout new_feature_1 branch and continue your work. If you want to use some new functionalites in urgent_feature, you can simply merge the master branch or urgent_feature branch.
You have a clean code base called master. Now you need to implement a new feature to your application. What you need to do is to create a new branch for this task, just like this: git branch new_feature_1. And then you can checkout this branch: git checkout new_feature_1. Now you have switch from master to new_feature_1, and you can start work on your feature on this branch.
If one day, while you are still in the middle of the new_feature_1, your manager comes and tells you 'hey, we have an urgent feature that must be done asap, and it has the highest priority'. You can simple save all your current work: git commit -a. This will commit all your work to the new_feature_1 branch. Then you can checkout master, the clean code base and create a new branch: urgent_feature.
Most people using CVS/SVN have painful experience with branch merging. GIT makes merging an incredibly simple and easy task. After you finish your work, you can simply merge it from master: git merge urgent_feature. Now, you can checkout new_feature_1 branch and continue your work. If you want to use some new functionalites in urgent_feature, you can simply merge the master branch or urgent_feature branch.
Monday, February 15, 2010
exec() vs system()
Besides all the descriptions from PHP manual, let us do a small test. Create a file called test.php:
echo 'hello';
Create another file called runexec.php:
exec('php test.php');
Create a runsystem.php:
system('php test.php');
All three files are located in the same directory. I found runexec.php cannot call test.php properly. I must use full path in the exec command. However, runsystem.php can work very well.
echo 'hello';
Create another file called runexec.php:
exec('php test.php');
Create a runsystem.php:
system('php test.php');
All three files are located in the same directory. I found runexec.php cannot call test.php properly. I must use full path in the exec command. However, runsystem.php can work very well.
Subscribe to:
Posts (Atom)