Skip to content

Hi Friends. From now onwards I will be posting my articles in http://shameerc.com . Thank you for visiting this Blog and Your support. I will be defenitly enjoying Your comments and queries.. Thank you..

An Introduction to Smarty 3

Please find this article on http://www.shameerc.com/2010/10/introduction-to-smarty-3.html

Abstract classes and Interfaces in PHP

We all are familiar with oop concept in php. Most of us will have a clear idea about classes and objects. Still there will be some points that confuses us, since we rarely use them (if you are not using any frameworks), like abstract classes and objects.  In this article we will discuss about the basic differences between Abstract classes and interface.

Classes

A class, conceptually, is a die that can be used for producing objects. We can say classes as a blue print for our objects that represents the real world entities. Classes generally include a set of statements – properties and methods- that performs some specific tasks.

Abstract class

An abstract class defines the basic structure of its child classes but cannot be instantiated by its own. Abstract classes can be surprisingly used for many applications even though we are not directly using an abstract class. The basic intention of using abstract class is to enjoy the benefits of inheritance but prevent the instantiation of parent class. An abstract class basically defines (and optionally  partially implements ) the interface for any classes that might extend it. It contains one or more abstract methods. By declaring a method as abstract we are enforcing other who extends this class to implement those methods, since all the abstract methods in an abstract class should be implemented by its child classes.

We can define an abstract class by simply using the keyword abstract before the keyword class. Likewise methods also can be declared abstract by using the keyword abstract. Suppose we need to develop a cms application. We know we are developing this system for managing different types of contents like blog, articles and comments. Whatever it may be, basically we will be managing contents. So we will have a base class named content. But we won’t be directly dealing with something called content. Instead we will have different types of contents like blog post, article, etc. Hence we will extend our base class content to create child class named Article. The coding will be as follows.

Our base class content will be

<?php
Abstract class content
{
abstract function save($content) ;
public function getContent($id)
{
//code to get contents
}
}
?>

Child class article will be as following

<?php

class Article extends Content

{

function save($content)

{

// code to save the article….

}

}

?>

In creating an abstract method make sure all its child classes implements them. Otherwise php will throw a fatal error.

Interfaces

As we have already discussed abstract classes allow us to provide a measure of implementation. Where as an interface is purely a template for our child classes. An interface will be simply defining methods and will not allow us to provide implementation in that class. An interface will contain a list of methods that must be implemented by its child classes. More over all methods must be public. Advantages of using interfaces are

–          An interface ensures that all classes that implements it will have a common set of functionality

–          Although a child class can have only one parent, it can implement multiple interfaces.

For example we can write an interface for a download link

Interface downloadable

{

public function getFileLocation();

public function createLink();

}

Example of a class that implements this interface

Class EbookDownloader  implements downloadable

{

public function getFileLocation()

{

// code to get file location

}

public function createLink()

{

//code to create link

}

}

In order for a class to implement multiple interfaces, we just separate them using comma. Any class that incorporates this interface commits to implement all the methods it defines or it must be declared abstract.

Where to use abstract class and interface

From the above description it will be clear where to user abstract classes and interfaces. An abstract class is useful when you use a substantial amount of functionalities to be same across the subclasses. For example, in a shopping cart application with multiple payment options we could have an abstract class with a generic payment method. Whereas we can use interface when we have unrelated items that need to provide some similar functionalities in a uniform way.

Getting started with smarty

Smarty is an open source template engine for php. It would be more accurately described as a “Template/Presentation Framework.”. One of Smarty’s primary design goals is to facilitate the separation of application code from presentation. Typically, the application code contains the business logic of your application, written and maintained in PHP code. This code is maintained by programmers. The presentation is the way your content is presented to the end user, which is written and maintained in template files. The templates are maintained by template designers.

At its basic, we will have template file in presentation layer (which will decide how data will be displayed in user side). The application code collects the content, assigns it to the template engine. Template engine compiles this code and displays it. Template files won’t be recompiles in next execution if they didn’t change. Also we can enable caching which will make execution even faster. Compiled file will contain mix of php and html codes.  Main advantages of using this template engine paradigm include
– Designers can’t break application code. They can mess with the templates all they want, but the code stays intact. The code will be tighter, more secure and easier to maintain.
-Errors in the templates are confined to the Smarty’s error handling routines, making them as simple and intuitive as possible for the designer.
-With presentation on its own layer, designers can modify or completely redesign it from scratch, all without intervention from the programmer.
It should be noted that smarty doesn’t completely prevent us from writing php codes inside template files. We include them inside {php}.. {/php} tags.

Lets see some of the features of smarty

  • Caching: Smarty provides fine-grained caching.
  • Configuration Files: Smarty can assign variables pulled from configuration files.
  • Easy to Use and Maintain: Web page designers are not dealing with PHP code syntax, but instead an easy-to-use templating syntax not much different than plain HTML.
  • Template Functions: Smarty provides a rich set of function using which we can perform a lot of tasks like looping, condition checking, etc.
  • Performance: Smarty performs extremely well, despite its vast feature set. Most of Smarty’s capabilities lie in plugins that are loaded on-demand.
  • Compiling: Smarty compiles templates into PHP code behind the scenes, eliminating run-time parsing of templates.
  • Plugins: One of the important feature is its plugin support. Smarty is highly extensible through this plugins. We can write our own plug-in without much pain. Valuators, Modifiers, etc can be written as a smarty plugin. This can be directly used in our template files where ever required. I will explain this in my future posts.

For starting a project using smarty, we should first download the smarty template engine.
Then we can proceed to our folder setup section.  Though the folder structure is quite flexible, it will be good to follow a standard structure for easy understanding. I will be discussing structure that I am following .

The first smarty folder is nothing but what we have downloaded from the smarty site. In the roo t next we will have a content (or application, name what ever you like) folder, Uploads( if uploading is there) and an index file. Our application will reside inside content folder. There we will have following directories

  • Class – All our class files will be kept in this folder
  • Compile – This folder is mandatory since smarty puts compiled files into this folde, inside which we can have cache directory also.
  • Engine – We keep our config file in this folder. Also we will be putting all our scripts that are used in whole site, in this folder.
  • Public- This directory will contain our plane php files. Files from this folders will bedirectory interacting with classes, and the result sets will be assigning to template files.
  • Skins – Inside this folder we keep our template files. We can have multiple set of template files here (if required). Inside the skin folder we will have folders for images, styles and javascript.

We can see an index.php file, only through which we will access our system. We will include smarty library files and other commonly required files like config file in index.php.

Writing efficient queries in mysql

In next few posts I will be discussing about writing efficient queries in mysql. Queries are the soul of an application. So writing queries efficiently is an inevitable task in application development. Query optimization basically starts from table designing. From a developers side, there are many things we need to focus to achive good performance, like
* Good table design
* Defining table relationships
* Choosing datatypes
* Choosing indices
* Writing queries that will take advantage of those indices

Good table designing and planning is an inevitable part of performance. Since without this we cant achieve a good performance.

Choosing Right data type

There are a number of datatypes supported by MySql. I dont think I should discuss them here. We should select appropriate datatype as per our requirement. Always our focus is to minimize the memory usage and hence to improve the performance. We should be aware of how much space each datatype will take. Some basic points to be remembered are
– Dont use lrger values if we smaller values can do it. Dont use Double if float will work fine.
– Use numeric datatypes rather string operations and datatypes, because numeric operations can perform faster than string operations. For example two integers can be compared faster than two string.
– We can use UNSIGNED INT rather than VARCHAR to store IP address.
– Use ENUM if we will have a limited number of values for a column.
– Define columns to be not null, since defining a field as NULL need one extra bit. Also MySql need not check if the field valu is null or not.

Jquery Mobile- Next generation plugin from jQuery

The “Write less, Do more” slogan is taking it to the next level with new jQuery mobile framework. This will one of the most promising development in jQuery’s history. Instead of focusing on writing unique applications for individual mobile devices jQuery mobile framework is looking forward to implement a common platform for all mobile devices.

Main focus of jQuery mobile project is to build dynamic touch interface that will adapt to range of mobile devices. The system will include both layouts (lists, detail panes, overlays) and a rich set of form controls and UI widgets

jQTouch- Touch of simplicity

jQTouch, A project by Sencha Labs , already implemented some basic functionalities using jquery ui concept. It include alert boxes, ajax operations etc. Though not adaptable with ubiquitous mobile devices, it will work in iPad, iPhone and some “forward thinking” mobile devices. This project is now hosted in Github

The “Write less, Do more” slogan is taking it to the next level with new jQuery mobile framework. This will one of the most promising development in jQuery’s history. Instead of focusing on writing unique applications for individual mobile devices jQuery mobile framework is looking forward to implement a common platform for all mobile devices.

Main focus of jQuery mobile project is to build dynamic touch interface that will adapt to range of mobile devices

Hello world!

Welcome to WordPress.com. This is your first post. Edit or delete it and start blogging!

Follow

Get every new post delivered to your Inbox.