Archive for June, 2009

php oops tutorial

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.
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;
}
}

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 ”
“; // It seems DIC likes to get rid of my HTML br tags
echo $zargento->showFlavor();
echo ”
“;
echo $zargento->showColor();

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:
PHP
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 ”
“;
echo $zargento->showFlavor();
echo ”
“;
echo $zargento->showColor();
echo ”
“;
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 ”
“;
echo $zargento->showFlavor();
echo ”
“;
echo $zargento->showColor();
echo ”
“;
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.
Hope you enjoyed it.

Redirection using htaccess

SEO 301 Redirect Single File

Redirect 301 /x/file.html /y/file.html

Redirect whole to new Domain

Redirect 301 / http://www.yourredirectingdomain.com

301 Redirect multiple files

RedirectMatch 301 /articles(.*) /$1

Redirect Entire website to single file

This is a 302 (temporary)redirect because its meant to point to a temporarily offline file.

RedirectMatch 302 ^/ /temporary-offline.html

Redirecting To Trigger Errors

Redirect 400 /seo/400

Redirect 401 /seo/401

Redirect 402 /seo/402

Redirect 403 /seo/403

Redirect 404 /index.php?error=404

Redirect 405 /seo/405

Redirect 406 /seo/406

Redirect 407 /seo/407

Redirect 408 /seo/408

Redirect 409 /seo/409

Redirect 410 /seo/410

Redirect 411 /seo/411

Redirect 412 /seo/412

Redirect 413 /seo/413

Redirect 414 /seo/414

Redirect 415 /seo/415

Redirect 416 /seo/416

Redirect 417 /seo/417

Redirect 418 /seo/418

Redirect 419 /seo/419

Redirect 420 /seo/420

Redirect 421 /seo/421

Redirect 422 /seo/422

Redirect 423 /seo/423

Redirect 424 /seo/424

Redirect 425 /seo/425

Redirect 426 /seo/426

Redirect 500 /seo/500

Redirect 501 /seo/501

Redirect 502 /seo/502

Redirect 503 /seo/503

Redirect 504 /seo/504

Redirect 505 /seo/505

Redirect 506 /seo/506

Redirect 507 /seo/507

Redirect 508 /seo/508

Redirect 509 /seo/509

Redirect 510 /seo/510

Kingston: The Indian cricket team kept its fingers crossed and geared up for the One-day series against the West Indies as the first ever Caribbean Games to be held in Trinidad and Tobago, slated for July, has been cancelled due to the outbreak of swine flu.
Trinidad and Tobago’s Health Minister Jerry Narace and Sports Minister Gary Hunt said the Games were cancelled to prevent the spread of the influenza A (HINI) virus that has so far spread to more than 76 countries and infected more than 30,000 people.
India, however, will not play any of the four One-dayers in the Port of Spain, the capital of Trinidad and Tobago. They will play two matches each in the neighbouring islands of Jamaica and St. Lucia.
Eighteen confirmed cases of the virus have been found in the twin islands of Trinidad and Tobago and health authorities have warned that the figure is likely to increase in the coming days.
The sports minister said that the cancellation of the Games was “undoubtedly a major disappointment for many fans who were anxiously anticipating exciting competition.”
“No doubt the athletes themselves will feel the greatest disappointment since they have been vigorously preparing to showcase their talents during the games. However, I am confident that they will understand the reasons for this cancellation and they will appreciate the wisdom behind the decision,” Hunt said.
The outbreak of swine flu also forced the Caribbean Football Union (CFU) to postpone the 2009 Girls’ and Boys’ Youth Cup scheduled to be hosted in Trinidad and Tobago in July and August.
Source: Indo-Asian News Service