Pages

Saturday, October 20, 2012

PHP - Classes And Inheritance


OOP stands for Object-Oriented Programming. It is a programming concept that caught on in the 1990's. OOP focuses on 'objects' which are, well, objects. They have certain characteristics, and can behave in certain ways. OOP programming has a few concepts that define it. One of the defining features we will start with is called a class.

A class shows what an object has and can do, and it consists of members. Members can be divided into properties and methods. Properties are the characteristics of the object. For example, cheese (object) has the properties of type (maybe Gorgonzola, or cheddar), color (green, or white), and flavor (awful or delicious). Methods are the actions and behaviors the object can do. For example, cheese (object) can mold. Now let's see the technical side of it.

class Cheese { // A class, shows what all cheese has
var $type; // These are a class's attributes, or properties
var $flavor; // They are sometimes called characteristics, too.
var $color; // All cheeses have these 3 properties

// These functions are called 'methods'
// It's what the cheese can do for you
// and what you can do for your cheese
function giveDetails ($thetype, $theflavor, $thecolor) {
$this->type = $thetype;
$this->flavor = $theflavor;
$this->color = $thecolor;
}

function showType() {
return $this->type;
}
function showColor() {
return $this->color;
}
function showFlavor() {
return $this->flavor;
}
}

You declare a class by using the word 'class'. It's common to define the properties first. You define properties by using 'var'. Next the methods are defined. When using any of the properties in your methods, you use the $this keyword. If I want to use the "flavor" property in a function, I would put $this->flavor.

Now let's see this class in action.

<?php

class Cheese { // A class, shows what all cheese has
var $type; // These are a class's attributes, or properties
var $flavor; // They are sometimes called characteristics, too.
var $color; // All cheeses have these 3 properties

// These functions are called 'methods'
// It's what the cheese can do for you
// and what you can do for your cheese
function giveDetails ($thetype, $theflavor, $thecolor) {
$this->type = $thetype;
$this->flavor = $theflavor;
$this->color = $thecolor;
}

function showType() {
return $this->type;
}
function showColor() {
return $this->color;
}
function showFlavor() {
return $this->flavor;
}
}

$zargento = new Cheese; // Zargento is a brand of cheese

// We will now give it characteristics
$zargento->giveDetails("Gorgonzola", "Awful", "Green and white");

// Now let's see those details
echo $zargento->showType();
echo "<br>"; // It seems DIC likes to get rid of my HTML br tags
echo $zargento->showFlavor();
echo "<br>";
echo $zargento->showColor();

?>


You declare a class by using the word 'class'. It's common to define the properties first. You define properties by using 'var'. Next the methods are defined. When using any of the properties in your methods, you use the $this keyword. If I want to use the "flavor" property in a function, I would put $this->flavor.

Now let's see this class in action.

You make a new object by using 'new'. $zargento is now a Cheese object. It has access to all the properties and methods we outlined in the class. If we want $zargento to use the giveDetails() function, you use '$zargento->giveDetails()' as was used above. If you run the above script, the output will be:


Gorgonzola
Awful
Green and white

Now that you've gotten a good idea of how classes work, we can move one step further with another concept of OOP called inheritance. This allows you to create new classes using already created classes. Here is an example:


class MoreCheese extends Cheese {
var $cost;

function giveCost($f) {
$this->cost = $f;
}

function showCost() {
return $this->cost;
}
}

You use 'extends' to grab the methods and properties from the Cheese class and add them to the MoreCheese class. Let's see the full code.

<?php

class Cheese { // A class, shows what all cheese has
var $type; // These are a class's attributes, or properties
var $flavor; // They are sometimes called characteristics, too.
var $color; // All cheeses have these 3 properties

// These functions are called 'methods'
// It's what the cheese can do for you
// and what you can do for your cheese
function giveDetails ($thetype, $theflavor, $thecolor) {
$this->type = $thetype;
$this->flavor = $theflavor;
$this->color = $thecolor;
}

function showType() {
return $this->type;
}
function showColor() {
return $this->color;
}
function showFlavor() {
return $this->flavor;
}
}

class MoreCheese extends Cheese {
var $cost;

function giveCost($f) {
$this->cost = $f;
}

function showCost() {
return $this->cost;
}
}

$zargento = new MoreCheese;
$zargento->giveDetails("Gorgonzola", "Awful", "Green and white");
$zargento->giveCost("23.39");
echo $zargento->showType();
echo "<br>";
echo $zargento->showFlavor();
echo "<br>";
echo $zargento->showColor();
echo "<br>";
echo $zargento->showCost();

?>

As you can see, even though $zargento is no longer Cheese, and is now MoreCheese, it still retains all of the methods and properties from Cheese because the MoreCheese class inherits all of them from Cheese. The advantages to inheritance are that you don't have to edit the base class (in this case Cheese). You can also override code, as this example shows.

<?php

class Cheese { // A class, shows what all cheese has
var $type; // These are a class's attributes, or properties
var $flavor; // They are sometimes called characteristics, too.
var $color; // All cheeses have these 3 properties

// These functions are called 'methods'
// It's what the cheese can do for you
// and what you can do for your cheese
function giveDetails ($thetype, $theflavor, $thecolor) {
$this->type = $thetype;
$this->flavor = $theflavor;
$this->color = $thecolor;
}

function showType() {
return $this->type;
}
function showColor() {
return $this->color;
}
function showFlavor() {
return $this->flavor;
}
}

class MoreCheese extends Cheese {
var $cost;

function giveDetails($q) {
echo $q;
}

function giveCost($f) {
$this->cost = $f;
}

function showCost() {
return $this->cost;
}
}

$zargento = new MoreCheese;
$zargento->giveDetails("dilly");
$zargento->giveCost("23.39");
echo $zargento->showType();
echo "<br>";
echo $zargento->showFlavor();
echo "<br>";
echo $zargento->showColor();
echo "<br>";
echo $zargento->showCost();

?>

As you can see, the giveDetails() function of the base class Cheese has been overrided by the giveDetails() function of the MoreCheese class. This is a very useful feature of inheritance.

Thank you for reading this part of the tutorial, and I hope you learned something

Installing a LAMP Stack on Ubuntu Using APT


This is to help people set-up and install a LAMP (Linux-Apache-MySQL-PHP) stack in Ubuntu, including Apache 2, PHP 5 and MySQL 5.0. You can actually do it with one line of commands but since that’s not the point, let’s go step by step.
Installing Apache 2

open up the Terminal and then type this line:

sudo apt-get install apache2 apache2-utils

To restart the server
sudo /etc/init.d/apache2 restart

Browse to http://localhost , you should see displayed a text message “it works”

By default, your document root folder is located at : /var/www/ . Out of the box, you won’t have write permission to this folder, so first of all we need to change that by changing the ownership of the folder to your user.

At a terminal, do the following (replacing salimane with your login name):

sudo chown -R salimane /var/www

By default, Ubuntu’s Apache 2 will ignore the directives in your .htaccess files. To make .htaccess files work as expected, you need to edit the file /etc/apache2/sites-available/default (sometimes /etc/apache2/sites-available/000-default)
Look for a section that looks like this:

<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
# Uncomment this directive is you want to see apache2's
# default start page (in /apache2-default) when you go to /
#RedirectMatch ^/$ /apache2-default/
</Directory>

You need to modify the line containing “AllowOverride None” to read “AllowOverride All”. This tells Apache that it’s okay to allow .htaccess files to over-ride previous directives.

You must reload Apache before this change will have an effect:

sudo /etc/init.d/apache2 reload

Installing MySQL Server

open up the Terminal and then type this line:

sudo apt-get install mysql-server

In order for other computers on your network to view the server you have created, you must first edit the “Bind Address”. Begin by opening up Terminal to edit the MySQL configuration file my.cnf .

gksudo gedit /etc/mysql/my.cnf

remove or comment out the line

bind-address = 127.0.0.1
and restart mysql server with :

sudo /etc/init.d/mysql restart

Installing PHP5

open up the Terminal and then type this line:

sudo apt-get install php5

Installing PHP5 module for apache2

open up the Terminal and then type this line:

sudo apt-get install libapache2-mod-php5

In order for PHP to work and be compatible with Apache we must restart it. Type the following code in Terminal to do this:

sudo /etc/init.d/apache2 restart

To ensure there are no issues with PHP let’s give it a quick test run. In the terminal copy/paste the following line:

echo "<?php phpinfo(); ?>" > /var/www/phpinfo.php

Now open your web browser and type the following into the web address: http://localhost/phpinfo.php

Installing MySQL module for PHP5

open up the Terminal and then type this line:

sudo apt-get install php5-mysql mysql-client

then edit the PHP configuration file php.ini and uncomment the following line by taking out the semicolon (;).
Change this line:

;extension=mysql.so

To look like this:

extension=mysql.so

Now just restart Apache and you are all set!

sudo /etc/init.d/apache2 restart

Installing PHPMyAdmin

One of the easiest ways to manage your new MySQL database server is to use the graphical tool PHPMyAdmin.
Simply go to your terminal again and enter the following command:

sudo apt-get install phpmyadmin libapache2-mod-auth-mysql

Restart Apache :

sudo /etc/init.d/apache2 restart

Now just point your browser at http://localhost/phpmyadmin/.

Installing some useful modules in PHP

If you have installed xampp for Linux you will see that by default they are already some modules installed for you . But unfortunately for the default lamp stack in Ubuntu it’s not, so let’s install those common used modules in php like gd,pear,curl,memcache,xmlrpc,xsl…

sudo apt-get install php5-dev php5-gd php-pear php5-curl php5-memcache php5-xmlrpc php5-xsl php5-imagick php5-mcrypt php5-mhash

Restart Apache

sudo /etc/init.d/apache2 restart

Use regular expressions in MySQL SELECT statements


A very cool and powerful capability in MySQL and other databases is the ability to incorporate regular expression syntax when selecting data. The regular expresion support in MySQL is extensive. This recipe reviews regular expression use in MySQL and lists the supported regular expression metacharacters.

The basic syntax to use regular expressions in a MySQL query is:

SELECT something FROM table WHERE column REGEXP 'regexp'

For example, to select all columns from the table events where the values in the column id end with 5587, use:

SELECT * FROM events WHERE id REGEXP '5587$'

A more elaborate example selects all columns of the table reviews where the values in the column description contain the word excellent:

SELECT * FROM reviews WHERE description REGEXP '[[:<:]]excellent[[:>:]]'

MySQL allows the following regular expression metacharacters:

. match any character ? match zero or one
* match zero or more
+ match one or more
{n} match n times
{m,n} match m through n times
{n,} match n or more times
^ beginning of line
$ end of line
[[:<:]] match beginning of words
[[:>:]] match ending of words
[:class:] match a character class
i.e., [:alpha:] for letters
[:space:] for whitespace
[:punct:] for punctuation
[:upper:] for upper case letters
[abc] match one of enclosed chars
[^xyz] match any char not enclosed
| separates alternatives

MySQL interprets a backslash (\) character as an escape character. To use a backslash in a regular expression, you must escape it with another backslash (\\).

Wednesday, October 17, 2012

Database Normalization - MySQL

What's Database Normalization ?

Normalization is the process where a database is designed in a way that removes redundancies, and increases the clarity in organizing data in a database.
In easy English, it means take similar stuff out of a collection of data and place them into tables. Keep doing this for each new table recursively and you'll have a Normalized database. From this resultant database you should be able to recreate the data into it's original state if there is a need to do so.
The important thing here is to know when to Normalize and when to be practical. That will come with experience. For now, read on...
Normalization of a database helps in modifying the design at later times and helps in being prepared if a change is required in the database design. Normalization raises the efficiency of the datatabase in terms of management, data storage and scalability.
Now Normalization of a Database is achieved by following a set of rules called 'forms' in creating the database.
These rules are 5 in number (with one extra one stuck in-between 3&4) and they are:

1st Normal Form or 1NF:

Each Column Type is Unique.

2nd Normal Form or 2NF:

The entity under consideration should already be in the 1NF and all attributes within the entity should depend solely on the entity's unique identifier.

3rd Normal Form or 3NF:

The entity should already be in the 2NF and no column entry should be dependent on any other entry (value) other than the key for the table.
If such an entity exists, move it outside into a new table.
Now if these 3NF are achieved, the database is considered normalized. But there are three more 'extended' NF for the elitist.
These are:

BCNF (Boyce & Codd):

The database should be in 3NF and all tables can have only one primary key.

4NF:

Tables cannot have multi-valued dependencies on a Primary Key.

5NF:

There should be no cyclic dependencies in a composite key. Trying to find website design company softnep ? Check out this page: http://www.softnep.com
Well this is a highly simplified explanation for Database Normalization. One can study this process extensively though. After working with databases for some time you'll automatically create Normalized databases. As, it's logical and practical.