Read the latest web development and design tips at Fred Wu's new blog! :-)
Poke me on GitHub

Archive for the ‘PHP’ Category

CodeIgniter – Loading Models and Database in One Hit

Most models I create need to access the database. You can easily have CI automatically load the database class by setting a 3rd parameter to true when loading your model:

$this->load->model('your_model', '', true);

The 2nd parameter, which in this case was left blank, is used if you want to assign a different object name to the model – something that I have never used.

Has anyone ever actually used a different object name? The reason I ask is because it’s so easy to forget about the 2nd parameter, and sometimes I try to put the true parameter to auto-load the database in the 2nd slot like this:

$this->load->model('my_model', true); // WRONG!

Of course, the easiest solution is to autoload the database through the config, but that can be a little inefficient for apps that don’t use the DB on every page.

Related posts

Release: [Kohana Module] Authlite, for User Authentication

Latest release: v1.2.3

Initially I was going to wait for my Layerful Framework (a transparent layering framework for Kohana) to mature before releasing its bundled modules. However, since this Authlite module can be used independently to Layerful, I am releasing it now.

What is Authlite

Authlite is a user authentication module for Kohana.

What’s the difference between Authlite and Auth?

Kohana comes with an official Auth module which does exactly what was described in the last paragraph, so why another module then?

It is because Authlite offers greater flexibilty. Please read the features outlined below to find more.

Features

  • Legacy database compatibility
  • Configurable database columns
  • Multiple instances of Authlite
  • Does not enforce user roles
  • Auth-like syntax and usage
  • Lightweight

(more…)

Related posts

Comparison: Ruby vs PHP, the Pros

Just to be clear, this is NOT a fanboyism post nor do I encourage the debate between which language is superior. My personal belief is to use the appropriate tool for appropriate projects.

PHP Pros

  • Widely available libraries (including PEAR and PECL)
  • An absolutely outstanding online manual
  • Relatively easy to learn (can hack together some code without deep understanding of the language)
  • Widely used (easy to find clients, etc)
  • Most if not all control panels have PHP integration
  • Development packages (XAMPP, WAMP, MAMP, LAMP, etc)
  • C style syntax (easier transition for people with C/C++/Java background)
  • Powerful array feature (Ruby’s equivalent to PHP’s associative array is Hash, which is not interchangeable with array)
  • PhpDoc is better than RDoc (this might be because I am too used to PhpDoc)

Ruby Pros

  • Fully object oriented
  • Rake (Ruby’s Make)
  • RubyGem (Ruby’s apt/yum)
  • Code blocks
  • ‘Ghost’ classes
  • Modules (aka namespace/package, PHP 5.3 will have namespace too, but with much uglier syntax)
  • lambda functions (PHP 5.3 will have this too, but not as powerful)
  • Children-aware parent classes (‘inherited’ hook)
  • Multi-inheritance through Mixins (PHP is single-inheritance)
  • Ruby 1.9 is unicode friendly (PHP 6 will be)
  • For what I do, Ruby on Rails > all PHP frameworks combined
  • Regular Expression built into the language core

I am pretty sure there’s heaps of pros for both PHP and Ruby missing from the list, but at least it gives you a rough idea on what to look for if you are not very familiar with them.

Please share your experience with us too (and I will update this post accordingly). :)

Related posts

Using Zend Framework with Kohana

Since my previous post on this topic, Kohana has evolved and changed quite a bit, to the point that the instructions provided are no longer applicable.

So here are the updated instructions for those who would like to integrate Zend Framework into Kohana.

1) Put the ‘Zend’ folder in your application’s ‘vendors’ directory.

2) Put the following code into an appropriate place in your application, it could be in the base controller.

if ($path = Kohana::find_file('vendors', 'Zend/Exception'))
{
	ini_set('include_path',
	ini_get('include_path').PATH_SEPARATOR.dirname(dirname($path)));
}

3) Instanciate the Zend library with the following code:

require_once 'Zend/Mail.php';
$mail = new Zend_Mail;

That’s it! How simple is that? :)

Related posts

json_encode() for PHP4 and early PHP5

Had a beauty today. There I was ready to deploy some nicely polished code to the server. After some thorough local testing it looked like it’d be a smooth process. So the code went up, but all my funky AJAX stuff stopped working on the server. How could that be? It was perfect locally… :)

It took a little while but in the end I realised what was going on – json_encode wasn’t working. The server was running PHP 5.1.6 and json_encode only became “standard” with PHP 5.2.0 onward.

I needed a solution fast. No time to recompile a newer version of PHP, add libraries or anything fancy like that. I just needed the function json_encode to work right now. Thankfully, the solution was as easy as adding replacement a function a user kindly submitted from the PHP site itself:

http://au.php.net/manual/en/function.json-encode.php#82904

I blindly assumed PHP 5 was PHP 5. I wasn’t using any extremely fancy commands or anything, but I still came unstuck. So the moral of today is check your server specification right down to every last decimal point! ;)

Related posts

PHP Namespace – $this->sux

Federico makes an interesting point about the PHP Namespaces Controversy on his PHP::Impact blog. I never thought about the mess that is PHP until I saw his comparison table:

Java:
Attribute/Method access: foo.bar
Static method access:    Foo.bar
Package access:          foo.bar.baz

C#:
Attribute/Method access: foo.bar
Static method access:    Foo.bar
Namespace access:        foo.bar.baz

Python:
Attribute/Method access: foo.bar
Static method access:    Foo.bar
Module access:           foo.bar.baz

PHP:
Attribute/Method access: $foo->bar
Static method access:    Foo::bar
Namespace access:        C:\foo\bar\baz

I guess I can’t complain though as the syntax of PHP is still a lot closer to more “traditional” languages than something like Ruby (not talking about the namespace, just the language in general)… :)

Related posts