The Deployer

April 14, 2009

Why should we use PHP constants inside classes

Filed under: Featured Articles, PHP — Tags: , , — Lucian Daniliuc @ 22:58
constant
A constant is an identifier (name) for a simple value. As the name suggests, that value cannot change during the execution of the script (except for magic constants, which aren’t actually constants). A constant is case-sensitive by default. By convention, constant identifiers are always uppercase.

Practically, it’s as easy as:

define('DATABASE', 'thedatabase');

Constants are useful in a lot of cases:

  • Configuration directives

    Storing database connection information is plain easy and available everywhere.

    define( 'DATABASE', 'thedatabase' );
    define( 'USERNAME', 'theuser' );
    define( 'PASSWORD', 'thepass' );
    define( 'HOSTNAME', 'localhost' );

    These have global scope, cannot be tempered with and can be accessed from everywhere. How easy is that?

  • Anti-hack techniques

    You can make sure nobody accesses and uses your files remotely by checking a constant that you only define in one place, unaccessible from the web.

    if(!defined('ANTIHACK_FG3N4890FN4334G3GH')) die('You naughty');
  • Various flags

    Usually you need here and there to set some flags that activate or dezactivate some functionalities in your application, including debug flags.

    define('USE_CACHE', true);
    //.... some code....
    if(constant('USE_CACHE')) {
      //... cache data here
    }

    By using

    constant()

    , we don’t get a notice if the constant is not defined, and to disable the cache, you can take any approach you find suited: either set the defined constant to “false”, or comment it, the outcome is the same and you don’t get notices and warnings in your code, even if you completely remove the constant from your code.

So, you define the constants with define(), you check to see if certain constant is defined with the defined() function and you get the value of a constant that could or could not be defined using constant().

Using constants in classes

Along with the wonders of OOP in PHP 5 came the possibility to define constants inside classes. For example:

class AppSettings {
	const DATABASE = 'thedeployer';
	const HOSTNAME = 'localhost';
	const USERNAME = 'deployer';
	const PASSWORD = 'thepassword';
 
	public function __construct() {
		echo 'database = `' . self::DATABASE . '`';
		echo 'hostname = `' . self::HOSTNAME . '`';
		echo 'username = `' . self::USERNAME . '`';
		echo 'password = `' . self::PASSWORD . '`';
	} // END constructor
} // END class
 
echo 'database from outside = ' . AppSettings::DATABASE;

Basically, the constants ar a part of the class definition, you don’t have to actually execute code to define the constants and they are available immediately for use. And most important, you don’t have to instantiate a class to access it’s constants, and you always know where to look for the definition.

The advantages and differences that one should remember when using constants and the reasons to use constants in classes rather than just floating around in your code are:

  • Constants defined in classes do not show up in the global scope.

    There is a stack where PHP holds all the constants and it’s a rather large list. It’s nothing like the variables which are a total of 4 superglobals that exists when a script starts. To see the list of defined constants, just run:

    print_r(get_defined_constants());

    You’ll get a huge list of approximately 1760 defined constants when your script hasn’t got a line. This is a required compromise, because these constants include TRUE, FALSE, error codes, constants defined by enabled extensions and some other useful constants. This is why your constants shouldn’t go here. If you define your constants as part of some classes, they remain within the class definition and don’t end up in the constants pool. And don’t worry, even if they’re defined within a class, they are available everywhere, just like a normal constant.

  • Grouping the same type of data together.

    Usually, you can define a class that just deals with every piece of configuration data your application needs, just like de example above. This means that all your constants are in one place, and if you need to change a constant, you only need to find where the class is defined and change your info. You even have the class name, because you’ll be using the constants defined in some class this way:

    echo 'database from outside = ' . AppSettings::DATABASE;
    // this means that the "DATABASE" constant is defined in the "AppSettings" class.

    How easy is that?

  • Available without executing code.

    Since the constants are part of the class definition, they’re available before even the first line of actual code is executed.

  • Class autoloading power.If you have the autoload feature in use in your application, you’ll get another advantage. You’ll have the constants defined with a single call to the wanted class that contain the constants. The autoloader will locate an load you class.

These are just a few things that need to be said and noted when addressing constants. Be careful not to use constants where they’re not appropriate. For example in internationalization. Don’t define the i18n labels in constants. That’s a very greedy approach. Use gettext extension or some other dedicated solution for this. Using constants is just bad.

Happy coding and have a nice day!

CodeIgniter 1.6 + Rapyd library download

Filed under: Downloads — Tags: , , , — Lucian Daniliuc @ 08:00

When I need to develop a fast-growing, flexible and complex management application that needs to be done in a very short time, I use a CodeIgniter – Rapyd “bundle” that I’ve built to work, which can be turned into a really complex application.

Actually, I’ve built a complex management application, and then stripped down all it’s business logic so that only the framework to build another remained.

As you may know, Rapyd is no longer being maintained for CI, but has moved to Kohana framework. So, if you need a solid framework to build you application and you don’t want to loose any time with the building blocks for basic functionality like displaying a data table, filtering, sorting, adding, modifying and deleting entries, just use this.

Download CodeIgniter 1.6 + Rapyd

Here are some screenshots of what can be done in just a few hours…

rapyd-screenshot1

rapyd-screenshot2

rapyd-screenshot3

Happy coding and have a nice day!

April 13, 2009

CSS Menu Generator

Filed under: CSS — Tags: , , — Lucian Daniliuc @ 20:57

cssmenumaker.com has quite a few ready-made CSS menus that just need to be copy-pasted in your site.

You can choose from drop-down menu, horizontal or vertical menu style and after that, you just copy-paste the code, or you can customize your menu.

Menus are usually compatible with Firefox, Internet Explorer 6 & 7, Safari and Opera.

April 12, 2009

How to create iPhone themes

Filed under: iPhone — Tags: , , , , — Lucian Daniliuc @ 10:45

I found a great website that teaches you how to make and use Winterboard themes.

hackthatiphone.com

hackthatiphone.com

This nice tutorial explains, step by step, how to:

Everything starts here:

http://www.hackthatphone.com/20/themes_with_winterboard.html

Happy theming and don’t forget to share your themes.

April 9, 2009

Unlocking (jailbreaking) your iPhone 3G (2.2.1)

Filed under: Featured Articles, iPhone — Tags: , , — Lucian Daniliuc @ 21:17

The background and “the why?”

Apple has a very evolved sense of security and thus you won’t be able to install any application not listed in the AppStore. In order for you to be able to install WinterBoard (a replacement for that main menu), themes, sounds, ringtones, wallpapers and all those goodies, you need to jailbreak your iPhone which means you have to make it be able to install any software you choose.

Each time you update to a new firmware version, a new QuickPWN tool is necessary. This article covers the update to 2.2.1, but it is exactly the same for any older version (I’ve tried them all).

Warning! This is not the same as unlocking your phone, which means making it work with any GSM operator out there. This article is not about this. Unlocking is illegal, while jailbreaking is legal and usually doesn’t break your warranty.

How to jailbreak your iPhone

1. Update

The first thing to do is have the latest version of iTunes installed in order to update your iPhone to the latest firmware. Just connect it to a internet-enabled computer with iTunes and you’ll instantly get the message to download and install the latest firmware, if you haven’t done so already.

2. Get the tool

Go to  http://www.quickpwn.com/2009/01/official-quickpwn-221-and-pwnagetool-jailbreak-for-windowsmac.html and download QuickPWN 2.2.1 for Windows. It’s small piece of software that does exactly this: jailbreak your phone.

QuickPWN Official website

QuickPWN Official website

When the 3.0 version will be out, this is where you should come and get the latest version.

A word of caution. I’ve seen that fresly releases of QuickPWN tools for fresh firmware versions are not fail-proof and you may run into trouble. You should wait for at least a month before jailbreaking your phone after a new version is released just to make sure that most of the bugs are identified and fixed. That is what I do.

3. Start the tool

Have the iPhone connected via USB cable to the computer and fire up the downloaded QuickPWN tool. It will show up like this:

QuickPWN Tool startup screenshot

QuickPWN Tool startup screenshot

Do not worry, the procedure is easy and it’s very unlikely that something will go wrong.

4. Firwmare file

Select the upgraded firmware. Once iTunes upgrades your iPhone firmware version to 2.2.1, it automaticaly saves the firmware somewhere on your drive. QuickPWN tools needs this file in order to patch it and reapply it to your iPhone.

If you have the file, is already selected in the textbox, if not, you can download it from here. After selecting it, or if you already had it selected, a progress circle starts spinning telling you that he’s working on patching the selected file. It does it in some backup file, so have no fear.

QuickPWN select firmware page

QuickPWN select firmware page

5. Touching the firmware file

After the tool patches the firmware file, you’ll see a big checkmark next to the phone picture. Click on the left arrow to go to the next step.

QuickPWN patched the firmware successfuly

QuickPWN patched the firmware successfuly

6. Select what you wish to install

Installing application will still be done from the iPhone, but there are two very nice applications to help you in doing that: Cydia and Installer. You should leave them both checked and go to the next page.

QuickPWN selecting Cydia and Installer

QuickPWN selecting Cydia and Installer

7. Check the USB connection

This is where things will start to be applied to your phone and for that check that the phone is correctly connected to the PC and click next.

QuickPWN checking the USB connection

QuickPWN checking the USB connection

During the jailbreaking you will be asked to keep the Start button pressed for a few seconds, then (while still keeping the Start button pressed) you will be asked to also push and keep pushed the power button for a number of seconds, and then, without releasing the Start button, to release the Power button and keep the Start button pressed for some more seconds.

Don’t panic, even if you don’t get it right the first time, you cand just try and try again. Just have your fingers correctly positioned and click next. This operations are required to put your iPhone in firmware upgrade mode, which cannot be done by software.

8. The jailbreaking

This is where the real process starts. As the iPhone connects in Recovery Mode you’ll see the firmware upgrade indicated on your phone.

iPhone in Recovery Mode

iPhone in Recovery Mode

After entering Recovery Mode, the jailbreaking procedure will start:

QuickPWN jailbreaking your iPhone

QuickPWN jailbreaking your iPhone

While the jailbreaking is in progress, your iPhone will show this screen:

QuickPWN jailbreaking almost complete.

QuickPWN jailbreaking almost complete.

This is going to take a few minutes. Don’t worry, this is perfectly normal.

Just before it ends, your iPhone will restart and show up a progress bar indicating the final touches:

QuickPWN reboots the iPhone and adds the final touch.

QuickPWN reboots the iPhone and adds the final touch.

9. Hurray! Your iPhone is out of it’s cage, and you can start benefiting from the tons and tons of applications, games, utilities and themes out there. How to install applications by using Cydia and Installer will be the subject of another article.

DISCLAIMER: I am not responsible if, for whatever reason, your phone, PC, ego or anything else is damaged or harmed in following this how-to. People all around the world jailbreak thousands of iPhones and have no problems, but you may have, for no apparent reason. Do this on your own risk!

Powered by WordPress