Admun's Tech Journey

thoughts, ideas, projects, and discoveries on technologies

  • Main
  • Tag Cloud
  • Archives

27

Nov

The Appreciation of foreach in PHP

Posted by Admun  Tags: php, language, syntax
I never realize this pitfall in PHP related to foreach until I revisit the manual when prepare for PHP certification:

$somearray = array('1', 2', '3', '4');
foreach ($somearray as $value) {
$total += $value;
$value += 10;
}

When dumping the contents of $somearray after the loop, the values are not changed..... I ran into this in a few occasions while working on NucleusCMS, but didn't realize until I read doc again in PHP5:

... On each loop, the value of the current element is assigned to $value and the internal array pointer is advanced by one (so on the next loop, you'll be looking at the next element). ...

The key here is "assigned". When the code changes $value, it actually got overwritten and discarded in the next iteration. The loop works properly for $total or if I only read from $value.

And yes, the pitfall was pointed out in the comments.... but who reads it? Certainly I didn't. cool

Because of this problem, there is a second form of foreach which provides the key as well. It turns out the second form is designed to fix just this pitfall:

$somearray = array('1', 2', '3', '4');
foreach ($somearray as $key => $value) {
$total += $value;
$somearray[$key] += 10;
}

Now, the contents in the array are changed after the loop.

Someone must have thought of a better way to handle this since PHP4. There is now a solution involving reference in PHP5:

$somearray = array('1', 2', '3', '4');
foreach ($somearray as &$value) {
$total += $value;
$value += 10;
}

The result is identical to the second form of foreach loop.

Sometimes, it's just not clear of the rational behind a language feature at first, like in this case (the second form of foreach). But once you understand the reason, it's much appreciated, especially when an elegant solution comes along (the reference in foreach).

Cool, eh?
Leave a comment

22

Nov

My Experience with CakePHP

Posted by Admun  Tags: php, cakephp, web app
My first project at L::G is built with CakePHP. It's been almost 3 months since I started using it. Here's some of my observations.

It's really a piece of cake!
I started with the tutorial. A few hours later, I gain enough confident and start working on the real project. It's just a simple tasks to add CRUD, but I already know how to do it after following the tutorial and look at code already in the real project.

Once I get a hang of MVC, it's a pleasure to work with; Define model that tie to database, access data with find(). Logic/algorithm goes to controller. Pass output to view via variables for output, which almost like NucluesCMS's skin.

It makes easy the things that matter the most
CakePHP has a very powerful mean to access data with containable. One find() often can scoop up all the related data and ready for action.

There are support to many common tasks; pagination, authentication, ACL, and etc. These build-in functions provide great help to web application developer.

Data submitted by user could contain data that harmful to the application, therefore, checking these data are an important task. However, CakePHP's Sanitize core library makes sanitizing in-coming data a snap.

Scafffolding is a powerful function in Rails application. CakePHP provides the same function and it's as easy as setting a var $scaffold in controller to get it to work.

The debugger function provides a great way to inspect the controller execution, It saves me a lot of echo to dump internal variables (but still have to set and pass them to view) .

After all, you are still a web application developer
It might be a disadvantage, but it does not bother me that there is no installation script. In order to get a CakePHP project up and running, there are only a few changes to make after unzip the package to webroot.

Along the same line, there is no tool to auto generate model/controller/view code to start with. But this generally mean I have to cut and paste some more code.

On the other hand, CakePHP's handle to model and database mapping is rather raw and bare. To create and add a model to my project, I have to manually create the database table. It feels like a car with manual transmission, I get to do all the things.

Of course, nothing is perfect
Through out the course of the project, I've ran into a few bumps.

It is not a big issue, but CakePHP imposed a strong naming convention for model/controller/view naming/database table/database columns, there are implicit behaviors associate with the convention. It trips me a few times at the beginning, but I get around it after I get use to it.

The documentation, according to a comment, is a great improvement. I found it's right to the point and easy to find information. However, what's good for documentation if important information is missing, or you need to read all the comments to learn a pitfall or realize doc? I can see there are still a lot of rooms to improve.

Meanwhile, the new and improved data validation still not mature enough (as of 1.2.5). i.e. range rule is non-inclusive. For a range check of 0-10, the code is actually 'rule' => array('range', -1, 11)...

Containable only work for second level, I have a few cases that need a third level containable with conditions. In those cases, I ended up have to do extra find() to get the needed data.

Performance, performance, performance. It's scary to see from the debugger so many extra SQL queries to get the job done.

There are still unanswered questions...
Even with some digging around, some questions still remain a mystery to me and I'm sure some re-factoring may come out because of these:
- What if I need transaction/rollback? Model data validation for associated models help, but is it enough? I'm having problem with cross-model data validation i.e. A question must has 2-5 answers - How do I enforce that?
- How to design architecture for functions that not quite fit into MVC model? i.e. SMS function is the prime example at hand.
Leave a comment

21

Nov

Getting Google Android SDK and App Engine SDK Installed on Fedora

Posted by Admun  Tags: google, android, app engine, sdk, fedora
Just installed Fedora 12 on my laptop, glad to see Eclipse updated to 3.5.1. But as usual, I have to jump through hops to get Android and App Engine SDK installed again. Sign.

First, I installed required Eclipse plugins available from Fedora (from GNOME System -> Admin -> Add/Remove Software):
eclipse-swt-3.5.1-4.fc12.i686
eclipse-platform-3.5.1-4.fc12.i686
eclipse-subclipse-1.6.5-1.fc12.noarch
eclipse-linuxprofilingframework-0.3.0-2.fc12.i686
eclipse-emf-2.5.0-4.fc12.noarch
eclipse-jdt-3.5.1-4.fc12.i686
eclipse-subclipse-graph-1.6.5-1.fc12.noarch
eclipse-emf-xsd-2.5.0-4.fc12.noarch
eclipse-rse-3.1-2.fc12.noarch
eclipse-svnkit-1.3.0-1.fc12.noarch
eclipse-phpeclipse-1.2.1-5.fc12.noarch
eclipse-cdt-6.0.0-10.fc12.i686
eclipse-rcp-3.5.1-4.fc12.i686
eclipse-gef-3.5.1-2.fc12.noarch
eclipse-callgraph-0.0.1-3.fc12.i686
eclipse-dtp-1.7.0-5.fc12.noarch

I installed a few extra for C/C++, PHP development and various things.

Then, install the SDKs and missing plugins from Eclipse's update manager (Help -> Install New Software):
Web Tools Platform - http://download.eclipse.org/webtools/updates/ (might have to skip Dali components....)
Google ADT plugin - https://dl-ssl.google.com/android/eclipse/
Google App engine plugin - http://dl.google.com/eclipse/plugin/3.5

After some waiting and a few restart. The two SDKs should be installed and working.
Leave a comment

About Me

admun My passion has always been on software development, and I know it since I wrote my first program on an Apple II. I worked on cellular wireless system in the past (C/C++) and now focus on web application (LAMP, PHP, MySQL, CakePHP, Symfony, jQuery, Google AppEngine/python).

Tags

abit android apache app engine audio bandwidth benchmark bing blogroll bluetooth boinc bookmark botnet bt cakephp cdma cellphone chinese chrome clouds coding crash crawler date dns drm dsl fedora friendster g1 gaim gdesklets gmail google grid h323 hardware html ie6 instant messaging internet javascript jquery language lifestreaming linux liunx meetup ming motorola msi mysql nas nat networking nokia ntp nucleus opinion optimization os p2p palm parrot php power reblog redhat regex rss sdk se search security shell skype social network spam stats string svn syntax sysadmin t-mobile teksavvy telecom thinkpad time tips tuning tv twitter unix voip web web app web2.0 webos wordpress

Archives

  • Full archive
  • May, 2013
  • Feb, 2013
  • May, 2012
  • Aug, 2011

Search

Powered by LMNucleus CMS v3.66 | Copyright Edmond Hui
This page takes 0.055 sec/29 queries to process | NP_BadBehavior blocked 334 spams for the past 7 days
Theme Design by short funny jokes | Ported to Nucleus CMS by BABOCHTA
[Valid XHTML 1.0 Strict]