Follow me in twitter

Preventing Cross-Site Scripting – PHP | Mypaaji http://ow.ly/1q62Zg


— Object Oriented Programming – PHP

Object – Oriented (OO) software development can be a confusing topic for developers who create primarily procedural code. But it doesn’t need to be. In this post, you’ll explore some of the basic theory behind OO and cover its (sometimes daunting) multisyllabic terminology.

What Is Object-Oriented Programming?

Object-Oriented Programming (OOP) requires a different way of thinking about how you construct your applications. Objects enable you to more closely model in code the real-world tasks, processes, and ideas that your application is designed to handle. Instead of thinking about an application as a thread of control that passes chunks of data from one function to the next, an OOP approach allows you to model the application as a set of collaborating objects that independently handle certain activities.

Understanding OOP Concepts

classes: the “blueprints” for an object and the actual code that defines the properties and methods.

objects: running instances of a class, that contains all the internal data and state information needed for your application to function.

inheritance: the ability to define a class of one kind as being a subtype of a different kind of class (much the same way as a square is a kind of rectangle).

polymorphism: allows a class to be defined as being a member of more than one category of classes (just as a car is “a thing with an engine” and “a thing with wheel”).

interfaces: defines a contract specifying that an object is capable of implementing a method without actually defining how it is to be done.

encapsulation: the ability of an object to protect access to its internal data.

Creating a Class

Start with a simple example. Save the following code in a file called class.Demo.php

<?php
class Demo
{

}
?>

Its important to have a clearly defined convention for organizing your source code file. A good rule to follow is to put each class into its own file and to name that file class.[ClassName].php

you can instantiate an object of type Demo like this

<?php

require_once('class.Demo.php');

$objDemo = new Demo();

?>

to instantiate an object, first make sure that PHP knows where to find the class declaration by including the file containing your class (class.Demo.php in this example).

Adding a Method

The Demo class isn’t particularly useful if it isn’t able to do anything, so let’s look at how you can create a method. Remember, a method of a class is basically just a function. By coding a function inside the braces of your class, you’re adding a method to that class. Here’s an example:

<?php

class Demo
{
function sayHello($name)
{
print "Hello $name!";
}
}

?>

An object derived from your class is now capable of printing a greeting to anyone who invokes the sayHello method. To invoke the method on your $objDemo object, you need to use the operator “->” to access the newly created function:

<?php

require_once('class.Demo.php');

$objDemo = new Demo();

$objDemo -> sayHello('Bajrangi Baba');

?>

The object is now capable of printing a friendly greeting. The “->” operator is used to access all methods the properties of your objects.

Adding a Property

Adding a property to your class is as simple as adding a method. You simply declare a variable inside the class to hold the value of the property.
Open the class.Demo.php file and do the respective changes:

<?php

class Demo
{
public $name;
function sayHello()
{
print "Hello $this->name!";
}
}

?>

The new variable, called $name, is all you have to do to create a property of the Demo class called name.
To access this property, you use the same “->” operator as that of the previous example, along with the name of the property.
Create a new file called testdemo.php and add the following:

<?php

require_once('class.Demo.php');

$objDemo = new Demo();
$objDemo -> name = 'Pison';

$objAnotherDemo = new Demo();
$objAnotherDemo -> name = 'Bajrangi Baba';

$objDemo -> sayHello();
$objAnotherDemo -> sayHello();

?>

Save the file and open it in your Web browser. The strings “Hello Pison” and “Hello Bajrangi Baba” print to the screen.
The Keyword public is used to let the class know that you want to have access to the following variable from outside the class.

Protecting Access to Member Variables

As the previous example shows, you can set the value of the name property to just about anything you want – including an object, an array of integers, a file handle or any other nonsensical value.
To work around this problem, always implement your properties in the form of function called get[property name] and set[property name]. Such functions are known as accessor methods, and are demonstrated in the following example.

Make the changes as the following to class.Demo.php

<?php

class Demo
{
private $_name;

public function sayHello()
{
print "Hello {$this -> getName()}!";
}

public function getName()
{
return $this -> $_name;
}

public function setName($name)
{
if(!is_string($name) || strlen($name)==0)
{
throw new Exception("Invalid Name Value");
}
$this -> _name = $name;
}
}

?>

Edit testdemo.php as shown here

<?php

require_once('class.Demo.php');

$objDemo = new Demo();
$objDemo -> setName('Pison');
$objDemo -> sayHello();

$objDemo -> setName(69);  //would trigger an error ; )

?>

As you can see, the member access level of name has changed from public to private and has been prefixed with underscore. The underscore is a recommended naming convention to indicate private member variables and functions; however its merely a convention – PHP does not require it.

Always use accessor methods for your properties. Change to business logic and data validation requirement in the future will be much easier to implement.

Initializing Objects

For many classes you will create, you will need to do some special set-up when an object of that class is firs instantiated. You might need to fetch some information from a database, or initialize some property values, for example. By creating a special method called a constructor, Implemented in PHP using a function called __construct(), you can perform any activities required to instantiate the object. PHP will automatically call this special function when instantiating the object.
For example you could rewrite the demo class in the following way:

<?php

class Demo
{
private $_name;
public function __construct($name)
{
$this -> _name = $name;
}
public function sayHello()
{
print "Hello $this -> _name";
}
}

?>

You can instantiate an object like as follows:

<?php

require_once('class.Demo.php');

$objDemo = new Demo('Pison');
$objDemo -> sayHello();

?>

That’s It guys:)


blog comments powered by Disqus