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')
Tuesday, March 2, 2010
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.
Sunday, November 8, 2009
Solution for "Can't update table in stored function/trigger because it is already used by statement which invoked this stored function/trigger"
I try to create a mysql trigger for testing:
DELIMITER $$
DROP TRIGGER IF EXISTS change_password $$
CREATE TRIGGER change_password
AFTER INSERT ON user
FOR EACH ROW
BEGIN
UPDATE user SET password = 'newpassword' WHERE id = NEW.id;
END
$$
DELIMITER ;
And then i do an insertion: "INSERT INTO user SET username='alice', password='changeme'"; I got this error:
"Can't update table user in stored function/trigger because it is already used by statement which invoked this stored function/trigger".
I search online and found a solution here: http://crazytoon.com/2008/03/03/mysql-error-1442-hy000-cant-update-table-t1-in-stored-functiontrigger-because-it-is-already-used-by-statement-which-invoked-this-stored-functiontrigger/
The point is, if you want to create a trigger on the table which will update itself, you must use the NEW.column_name to refer to the row. So i modify my trigger as:
DELIMITER $$
DROP TRIGGER IF EXISTS change_password $$
CREATE TRIGGER change_password
AFTER INSERT ON user
FOR EACH ROW
BEGIN
SET NEW.password = 'newpassword';
END
$$
DELIMITER ;
This time i got another error when i create this trigger: "Updating of NEW row is not allowed in after trigger". So i change the trigger to:
DELIMITER $$
DROP TRIGGER IF EXISTS change_password $$
CREATE TRIGGER change_password
BEFORE INSERT ON user
FOR EACH ROW
BEGIN
SET NEW.password = 'newpassword';
END
$$
DELIMITER ;
The trigger is created successfully and being executed when i run the insert sql statement again.
DELIMITER $$
DROP TRIGGER IF EXISTS change_password $$
CREATE TRIGGER change_password
AFTER INSERT ON user
FOR EACH ROW
BEGIN
UPDATE user SET password = 'newpassword' WHERE id = NEW.id;
END
$$
DELIMITER ;
And then i do an insertion: "INSERT INTO user SET username='alice', password='changeme'"; I got this error:
"Can't update table user in stored function/trigger because it is already used by statement which invoked this stored function/trigger".
I search online and found a solution here: http://crazytoon.com/2008/03/03/mysql-error-1442-hy000-cant-update-table-t1-in-stored-functiontrigger-because-it-is-already-used-by-statement-which-invoked-this-stored-functiontrigger/
The point is, if you want to create a trigger on the table which will update itself, you must use the NEW.column_name to refer to the row. So i modify my trigger as:
DELIMITER $$
DROP TRIGGER IF EXISTS change_password $$
CREATE TRIGGER change_password
AFTER INSERT ON user
FOR EACH ROW
BEGIN
SET NEW.password = 'newpassword';
END
$$
DELIMITER ;
This time i got another error when i create this trigger: "Updating of NEW row is not allowed in after trigger". So i change the trigger to:
DELIMITER $$
DROP TRIGGER IF EXISTS change_password $$
CREATE TRIGGER change_password
BEFORE INSERT ON user
FOR EACH ROW
BEGIN
SET NEW.password = 'newpassword';
END
$$
DELIMITER ;
The trigger is created successfully and being executed when i run the insert sql statement again.
Wednesday, August 5, 2009
外汇学习
最近开始对炒外汇感兴趣, 先来说说为什么要炒外汇, 也就是炒外汇的好处吧. (一开始就说风险不把别人吓跑才怪) 以下都是从网上收集来的:
1. 交易成本低,无需佣金
外汇投资者无需通过代理机构来进行交易,因此不会被收取交易佣金。外汇交易商的利润仅从报价点差中来。如 EUR/USD 1.4225/1.4227,其中仅有的 2 个点的点差就是交易者的成本。股票交易通常会收取 $7 至 $20 不等的交易佣金(也有根据交易量大小计算费用),买进卖出一个来回,佣金将被收取两次。相对而言,外汇的交易成本要小得多。
2. 无论熊市牛市, 获利机会均等
在股市中,投资者如果想卖空(Short)利用熊市来获利,将面对诸多限制。如较高的保证金要求 (通常是卖空仓位市值的 150% 左右)、提高报价规则(uptick rule)以及借贷成本等。而外汇的做空机制非常灵活,没有任何限制,外汇交易者能自由地借助市场的涨势和跌势来投资获利。
3. 24小时全球交易
外汇市场是一个 24 小时永不停滞的全球市场,交易者可以根据自己的生活习惯安排交易时间。这也是为什么众多上班族选择炒汇的原因之一。同时,越来越多的人开始利用股市休市的时间交易外汇,把其当作一项分散投资风险的有效渠道。
4. 高至200:1的杠杆(据个人了解外汇杠杆可以高达400:1甚至500:1)
外汇交易中提供的杠杆比率通常是股票交易的 100 倍。比如,在股市中投资者能用 $1,000 来买价值 $2,000 的股票,而通过高达 200:1 的杠杆,外汇交易者可以用同样的 $1,000 来获得 $200,000 的购买力。显然,外汇交易以小博大的效力,是远胜于股票的。
当然,我们必须认识到,杠杆比率是一把双刃剑,它能使投资者迅速获利,同时也很容易增加风险。
5. 瞬间交割无限制
外汇市场是短线投资者的乐园,因为交易者能在一日内自由进出场,瞬间即可成交,随时兑现盈利。而股票交易者若想进行短线交易(Day Trading),将面对诸多限制,如交易者必须满足 $25,000 的最低保证金要求等。
6. 8000支股票 VS 7种主要货币
纽约证交所和那斯达克两所股市中有 8000 支以上的股票,面对繁多的市场杂讯,选股成为一件令投资者头痛的事。而在外汇市场中,7 种主要货币集中了 85% 的交易量,这使交易者能更快选择投资对象,节约时间和资金。
1. 交易成本低,无需佣金
外汇投资者无需通过代理机构来进行交易,因此不会被收取交易佣金。外汇交易商的利润仅从报价点差中来。如 EUR/USD 1.4225/1.4227,其中仅有的 2 个点的点差就是交易者的成本。股票交易通常会收取 $7 至 $20 不等的交易佣金(也有根据交易量大小计算费用),买进卖出一个来回,佣金将被收取两次。相对而言,外汇的交易成本要小得多。
2. 无论熊市牛市, 获利机会均等
在股市中,投资者如果想卖空(Short)利用熊市来获利,将面对诸多限制。如较高的保证金要求 (通常是卖空仓位市值的 150% 左右)、提高报价规则(uptick rule)以及借贷成本等。而外汇的做空机制非常灵活,没有任何限制,外汇交易者能自由地借助市场的涨势和跌势来投资获利。
3. 24小时全球交易
外汇市场是一个 24 小时永不停滞的全球市场,交易者可以根据自己的生活习惯安排交易时间。这也是为什么众多上班族选择炒汇的原因之一。同时,越来越多的人开始利用股市休市的时间交易外汇,把其当作一项分散投资风险的有效渠道。
4. 高至200:1的杠杆(据个人了解外汇杠杆可以高达400:1甚至500:1)
外汇交易中提供的杠杆比率通常是股票交易的 100 倍。比如,在股市中投资者能用 $1,000 来买价值 $2,000 的股票,而通过高达 200:1 的杠杆,外汇交易者可以用同样的 $1,000 来获得 $200,000 的购买力。显然,外汇交易以小博大的效力,是远胜于股票的。
当然,我们必须认识到,杠杆比率是一把双刃剑,它能使投资者迅速获利,同时也很容易增加风险。
5. 瞬间交割无限制
外汇市场是短线投资者的乐园,因为交易者能在一日内自由进出场,瞬间即可成交,随时兑现盈利。而股票交易者若想进行短线交易(Day Trading),将面对诸多限制,如交易者必须满足 $25,000 的最低保证金要求等。
6. 8000支股票 VS 7种主要货币
纽约证交所和那斯达克两所股市中有 8000 支以上的股票,面对繁多的市场杂讯,选股成为一件令投资者头痛的事。而在外汇市场中,7 种主要货币集中了 85% 的交易量,这使交易者能更快选择投资对象,节约时间和资金。
refactoring and performance
Many programmers always put performance at the first place in development. However, in my opinion, performance should be the last and least thing we need to worry about. Let's have a look at what Martin Fowler says in his book - Refactoring.
"To make the software easier to understand, you often make changes that will cause the program to run more slowly...but it also makes the software more amenable to performance tuning. The secret to fast software...is to write tunable software first and then to tune it for sufficient speed."
"Changes that improve performance usually make the program harder to work with"
"Build your programm in a well-factored manner without paying attention to performance until you begin a performance optimization stage, usually fairly late in development."
"A well-factored program ... gives you time to spend on performance tunning... Because the code is clearer, you have better understanding of your options and of what kind of tuning will work."
I always believe the first task for developer is to make the software easy to maintain, extend, modify. It also means we should focus more on architecture, structure, design, and coding style.
Unfortunately most of performance-first developers haven't realized that their performance achieved coding creates buggy, hard to understand and maintain system and finally achieves nothing but lost.
Why do we not just use assembly language for programming? We can get better performance than using C++, Java, PHP.
Why do we prefer OOP instead of coding in native language way? Native PHP language code runs faster than OOP code with those classes, objects.
Why do we break code into smaller pieces and functions? Putting everything in one function can save a lot of time than calling other functions and then return to the caller.
Why do we use template engine? Why not just embed PHP into HTML? It is faster.
Why do we use framework? It absolutely runs slower than a index.php.
Why do we use java? Every performance oriented programmer is crying Java is slow.
"To make the software easier to understand, you often make changes that will cause the program to run more slowly...but it also makes the software more amenable to performance tuning. The secret to fast software...is to write tunable software first and then to tune it for sufficient speed."
"Changes that improve performance usually make the program harder to work with"
"Build your programm in a well-factored manner without paying attention to performance until you begin a performance optimization stage, usually fairly late in development."
"A well-factored program ... gives you time to spend on performance tunning... Because the code is clearer, you have better understanding of your options and of what kind of tuning will work."
I always believe the first task for developer is to make the software easy to maintain, extend, modify. It also means we should focus more on architecture, structure, design, and coding style.
Unfortunately most of performance-first developers haven't realized that their performance achieved coding creates buggy, hard to understand and maintain system and finally achieves nothing but lost.
Why do we not just use assembly language for programming? We can get better performance than using C++, Java, PHP.
Why do we prefer OOP instead of coding in native language way? Native PHP language code runs faster than OOP code with those classes, objects.
Why do we break code into smaller pieces and functions? Putting everything in one function can save a lot of time than calling other functions and then return to the caller.
Why do we use template engine? Why not just embed PHP into HTML? It is faster.
Why do we use framework? It absolutely runs slower than a index.php.
Why do we use java? Every performance oriented programmer is crying Java is slow.
Thursday, July 2, 2009
中国试行社会养老
中国终于要实行社会养老了:http://news.sohu.com/20090702/n264931053.shtml
先在部分地方试行,然后覆盖全国.实行这项制度的意义是不言而喻的.养儿防老在中国农民家庭中是根深蒂固的.这也是为什么很多农民家庭一定要生儿子,生男孩,生很多男孩的原因之一(另一个原因是要增加劳动力).这不能怪他们,因为中国农村生产力低下,而社会又不能为农民的老年提供保障.他们就只能指靠儿子了.
实行社会养老是一个重大改变.是完善社会保障制度的一个重要举措.虽然实施起来肯定会遇上不少困难,但一定要坚持下去,真正做到老有所养.
先在部分地方试行,然后覆盖全国.实行这项制度的意义是不言而喻的.养儿防老在中国农民家庭中是根深蒂固的.这也是为什么很多农民家庭一定要生儿子,生男孩,生很多男孩的原因之一(另一个原因是要增加劳动力).这不能怪他们,因为中国农村生产力低下,而社会又不能为农民的老年提供保障.他们就只能指靠儿子了.
实行社会养老是一个重大改变.是完善社会保障制度的一个重要举措.虽然实施起来肯定会遇上不少困难,但一定要坚持下去,真正做到老有所养.
Subscribe to:
Posts (Atom)