Object Oriented Programming in PHP5 (Part I)

May 7th, 2008

PHP6 is coming closer and closer and it will enrich the famous programming language with lots of cool features. One of the areas that will benefit the most from this new release is the Object Oriented Programming (OOP) part. This area has developed in an accelerated manner so the language can catch with other programming languages like Java, C++ or C#.

Looking back in time at PHP4, we see a relatively weak programming language when it comes to OOP. PHP4 implemented support for classes, inheritance and object serialization methods, but that was kinda it. Of course, it had a few other small features (like the parent variable inside each class), but most of the important ones were missing and made developing famous design patters like Singleton or Factory impossible.

But then came the knight-in-shiny-armor called PHP5 to the rescue. Most of the needs of programmers were covered with this release.

In this article, we will take a look at the most important (and useful at the same time) features that were added into PHP5.

For starters, let’s mark the changes:

1) Constructors
Constructors used to be declared as a function with the same name as the class’. For example, if you had a class called MyClass, you would have to create a method called MyClass(), which would be used as a constructor. The problem with this approach came when you wanted to change a class’ name: you would have to remember to change the name of the constructor each time.
In PHP5, however, the way the constructors are declared has changed, so that it is independent of the class name. To declare a constructor in PHP5, just declare a method called __construct() inside the class.
For backwards compatibility, you can still use the old constructor style in PHP5, but it is discouraged.
Note: Neither PHP4 or PHP5 call the parent constructor from the constructor of the child class. It is up to the user to do this. You can do it using the resolution operator (Paamayim Nekudotayim) like this: parent::__construct() or parentClassName::__construct().

2) Destructors
Destructors were not available in PHP4, but with PHP5 they can now be invoked using the special magic method: __destruct().
For the sake of consistency, you should try declaring constructors and destructors using PHP5 style. (Never use the PHP4 style constructor and the destructor provided in PHP5).

Now, let’s get to the really nice part: the new features of PHP5.

1) Visiblity property
This was one of the key features missing from PHP4: you could not declare private or protected members inside a class. They were all public.
However, in PHP5 you can safely use these properties and be sure they will work the right way. The available keywords are (according to the official PHP5 manual):
- public: The resource can be accessed from any scope.
- protected: The resource can only be accessed from within the class
where it is defined and its descendants.
- private: The resource can only be accessed from within the class
where it is defined.
- final: The resource is accessible from any scope, but cannot be
overridden in descendant classes.

Examples:
If you want to have a protected variable inside a class, just declare it like this: private $var = ‘12′;
If you want a protected function, here is how to do it: protected my_function()

Please not that you must specify the visibility before each member of the class (the implicit state is public), unlike in other languages, where you could group several members with a single keyword.

2) Autoloading
Sometimes, it is quite time-consuming to load all the classes you could possibly need in your application and it would be a lot faster if you could load only the ones you need. PHP5 comes with a solution to this: the __autoload() function. The function takes one parameter, which is the class name.
Here is a sample definition of such a function:

function __autoload($className) {
require_once $className . ‘.class.php’;
}

3) Static members
This is a key feature in developing famous design patterns like Singleton or Factory. Basically, having a static member inside a class means that it will be accessible without an instance of that class.
Please note that the $this variable is not available inside methods declared as static.
Using the 1) and 3) additions to PHP5, we can now implement a Singleton sample class:

class MySingleton
{
static $instance;
private $data;

private function __construct($data) {$this->data = $data;}
private function __clone() {}

public static function getInstance()
{
if ($instance instanceof MySingleton) {
self::$instance = new MySingleton(23);
}

return self::$instance;
}
}

These were only a few features that come with PHP5 and look what a splendid class we built using them: one that can be instantiated only once! Any other attempt to get an instance of it will return the already existing instance. Neat, huh? Stay tuned for part two of the tutorial, I will show you some even cooler stuff!

Daia Lucian

Marketing Definitions

December 11th, 2007

There are some marketing definitions that we apply when we take on a client or project of building a website. These definitions will help to improve any marketing strategy or effort.

Marketing: the creation of a product (or service) and taking it to the market to sell.

Market: the place(s) where buyers and sellers are. The location(s) where one can sell product(s) or service(s) such as by way of the Internet or by bricks and mortar (physical locations such as stores).

Marketing can be seen to be a series of steps whereby one:

  1. conceives of a product or service
  2. determines the viability of that product or service (if people want this product or service and what might they pay for it)
  3. finds out what people think about that product (such as what people want, need or demand from that product or service so that you can effectively sell that product or service)
  4. develops an online (or offline) presence that represents what your customers or clients think about the product or service, develop a doable marketing campaign
  5. packages the product or service so as to attract customers or clients (such as a website or print ad)
  6. offers the product or service
  7. sells the product or service
  8. delivers the product or service

Once done, you then improve the above steps so that there are more sales and more income. There can be many more steps; the above is not a complete list.

Basically, our definition of Marketing is: potentially, we could be responsible from the origination of a product or service all the way through to the delivery of the product or service. Usually, this isn’t the case with a website. For example, we are usually hired to help sell an already existing product or service via the Internet. But we put every project through the same lineup of marketing steps. There are fundamentals to marketing, and those fundamentals of Marketing apply before the fundamentals of the creation of a website — because the website is the “tangible” conception of the marketing plan.

If you analyze each project first from a marketing viewpoint, and then determine how best to sell the product or service on the Web, you will do a better job of marketing your product or service than if you, say, simply created a website without addressing how to market, or how to build your market.

Blog vs. CMS

December 11th, 2007

Blogs vs. CMS
First, let me start by defining a blog:

A blog is a web-based software that allows users to create web pages through a web interface - you enter text into an HTML form and that text is placed into pages.

… The pages that you add to a blog, are date ordered and (are typically) stored in an SQL database .. like MySQL for example.

From the popular Wordpress site:

A blog is a frequently updated, personal website featuring diary-type commentary and links to articles on other Web sites. Blogs range from the personal to the political, and can focus on one narrow subject or a whole range of subjects.

Blogs have been created with several languages: PHP, Perl, Python etc. Wordpress for example, is written in PHP.

… Actually, PHP is probably the most commonly used language for building blog and CMS software.

So what is a CMS?

In a nutshell: a CMS does everything a blog does … and a whole lot more.

Blogs are typically designed for one or two people to use. Whereas a CMS is designed to handle communities.

Blogs = few contributors/writers
CMS = many contributors/writers.

CMS software will have a lot more capability in terms of what kind of activities you can do with it:

  • User polling.
  • Built in blogs.
  • Built in Forums.
  • Configurable, precise control over what users can do.

.. And much more.

All this extra functionality comes with a cost: CMS software is more complex to learn and use. I have also found that CMS softwares tends to run slower than their nimble cousins - blogs.
Choosing between a blog or CMS

Choosing between a blog and a CMS can be a painful experience - as it was for me. But in the end, you have to approach it by figuring out what you really need to do on your website … and then look at your options in the blog and CMS world.

In my case, I narrowed it down to Drupal (a CMS) and a blog, Wordpress. After having installed and configured both (to get a feel,) I went with Wordpress because it was fast and it met my needs.