Wednesday, October 6, 2010

classes and objects in PHP

Hi Friends,

Today i am here to present you

The hardest thing to learn (and teach btw,) in object oriented PHP … is the basics. But once you understand them, the rest will come much, much easier.

Classes and Objects are considered to be the most useful and dynamic aspects of a programming language.

Classes:

In PHP, classes are used extensively and are very useful. The concept of classes allows for better performance and more features.

Let me tell you what a class is??

The Class in PHP is basically the same as in other languages such as Java. The class definition begins with the keyword class, followed by a class name.

The form name can be any name except a reserved word or keyword in PHP. The class name is followed by a pair of curly braces which contain the definition of class members and methods.

Look at the example below.



Objects??

An Object is an enclosed bundle of variables and functions which is copied from a Class.

Objects provide an easy interface and hide a lot of their inner workings.

The object sends orders through special functions called methods and they can return information.

The class abc is the basis from which many objects can be instantiated. The new keyword is used to create an object.

Below simple Example shows who to use php classes and objects in php with html.
In the following example $obj1 and $obj2 are the objects of the class abc. In this $obj2 has been assigned the string “Welcome to My blog!!” to its $a property.

a = "Welcome to My blog!!";

echo "$obj1->a";
echo "$obj2->a";
?>

Note:
The new keyword is used to create an object. Now any abc object that is created contains a property called $a with the value of “Hello”. This property can be accessed and even be changed with the help of objects.


Extends



Classes and Objects are considered to be the most useful and dynamic aspects of a programming language. In PHP, classes are used extensively and are very useful. The concept of classes allows for better performance and more features.

What is a Class?
The Class in PHP is basically the same as in other languages such as Java. The class definition begins with the keyword class, followed by a class name. The form name can be any name except a reserved word or keyword in PHP. The class name is followed by a pair of curly braces which contain the definition of class members and methods.



Example of a Class






Class abc

{

//member functions and variables go here

}



?>


What is an Object?
An Object is an enclosed bundle of variables and functions which is copied from a Class. Objects provide an easy interface and hide a lot of their inner workings. The object sends orders through special functions called methods and they can return information.

While creating a Class, a set of characteristics is laid down. By creating Objects of that type, entities are created that share these characteristics but the Object might initialize them as different values.



Example



Suppose there is a class named building. This class would have a characteristic named floor. All the objects of class building would share the characteristics of floor, but some would initialize it to “one”, some to “two”, others to “three” or “four”, and so on.


The benefit of object oriented code is that it is re-useable. In this the classes can be used to create different objects and classes from one project can be used in other projects as well. Child classes can also be created which inherits the properties of the parent classes.

Creating an Instance
To start with, a class having no member functions and variables is not useful. For a class to be completely useful, member functions and variables have to be added in that class.

Let’s take an example of a class with a variable in it.



Example






Class abc

{

$a = “Hello!”;

}



?>


The class abc is the basis from which many objects can be instantiated. The new keyword is used to create an object. Now any abc object that is created contains a property called $a with the value of “Hello”. This property can be accessed and even be changed with the help of objects.

In this the -> operator is used to access or change the properties of the object.

In the following example $obj1 and $obj2 are the objects of the class abc. In this $obj2 has been assigned the string “Welcome to expertrating!” to its $a property.

Example












Class abc

{

var $a = "Hello";

}



$obj1 = new abc();

$obj2 = new abc();

$obj2->a = "Welcome to expertrating!";

echo "$obj1->a
";

echo "$obj2->a
";



?>








Extends

Another feature of object oriented programming is used in PHP, which is inheritance. In PHP a class a class can inherit methods, functions and members of other class by using the extends keyword in the declaration. In PHP it is not possible to inherit from multiple classes, a class can inherit from only one base class.

The class from which inheritance is done is called the parent class or base class and the class which inherits is called the child class.

1 comment:

  1. can u please explain

    see first $a="hello";

    again

    $obj2->a="welcome to expertraining";

    but in

    echo "$obj1->a";
    echo "$obj2->a";

    am not understanding what is the meaninig of "->"

    ReplyDelete