IT Community - Software Programming, Web Development and Technical Support

PHP Classes For Beginners

This is a discussion on PHP Classes For Beginners within the PHP Programming forums, part of the Web Development category; Introduction Before we learn to code the oops in PHP we must understand the basics of classes and objects. Now ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > PHP Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 04-17-2008, 06:36 AM
jegan jegan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 161
jegan is on a distinguished road
Default PHP Classes For Beginners

Introduction
Before we learn to code the oops in PHP we must understand the basics of classes and objects. Now i have explained what is objects.
Objects

1. Objects are a run time entity.
2. An object is a collection of different data types.
3. An object contains two parts called STATE and BEHAVIOR.
4. The state is represented by the member variables.
5. The behavior is represented by the member functions.
6. And the objects have a unique name to differentiate from other objects
__________________
Thanks & Regards,
Jegan CBK
"We will either find a way, or make one!”

Last edited by jegan : 04-17-2008 at 06:38 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-17-2008, 06:37 AM
jegan jegan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 161
jegan is on a distinguished road
Default Re: PHP Classes For Beginners

In last post we have learned what is objects now we are going to see what is classes.
Classes

1. classes are compile time entity
2. Class is a template for an object.
3. It defines the state and behavior of an object.
4. The state is represented by the member variables
5. And the behavior is represented by the member functions.
6. The class is cannot used directly on run time, it’s just used to create the objects and the objects only used in run time.
7. A class can be used to create numerous objects.
8. In other word we can say the class is a new data type or user defined data types which are created by the user. We can create new variables (originally called instance or objects) by using these user defined data types.
__________________
Thanks & Regards,
Jegan CBK
"We will either find a way, or make one!”

Last edited by jegan : 04-17-2008 at 06:39 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-17-2008, 06:40 AM
jegan jegan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 161
jegan is on a distinguished road
Default Re: PHP Classes For Beginners

C Structure and classes

In c we could learn the structures. We can say that the objects are advanced version of structure. The structure are also defines the collections of multiple data types. But the classes create objects with collection of multiple data types with the member functions.
__________________
Thanks & Regards,
Jegan CBK
"We will either find a way, or make one!”
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-17-2008, 06:51 AM
saravanan saravanan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 181
saravanan is on a distinguished road
Default Re: PHP Classes For Beginners

i want know more about classes and object in php
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-18-2008, 06:29 AM
jegan jegan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 161
jegan is on a distinguished road
Default Re: PHP Classes For Beginners

Hi saravanan i have created a sample class structure for u, by using this u can understand to build a simple class. Now i have written about the components of class.

Creating Classes in PHP

1. Every class definition begins with the keyword class,
2. Then the name of the class name
3. then pair of curly brackets like { }
4. We can define member variables and member methods between these curly brackets

Example Class structure

Class Class_name
{
var member_variable1
var member_variable2
..
..
var member_variablen

function memner_function1()
{
}
…
….
function memner_functionn()
{
}
}
__________________
Thanks & Regards,
Jegan CBK
"We will either find a way, or make one!”
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 04-18-2008, 06:35 AM
jegan jegan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 161
jegan is on a distinguished road
Default Re: PHP Classes For Beginners

An example

Objects have been created to compare with real time objects like anything u are seeing. Now I have explained the oops concept using the object BOX. I am going to create a class to create objects like BOX.

Member Variable in Box Object
An object state is defined by the member variable. In BOX object the states are width and the height.

var width;
var height;

class
PHP Code:
class Box
{
    var  
width;
    var 
height;

__________________
Thanks & Regards,
Jegan CBK
"We will either find a way, or make one!”

Last edited by jegan : 04-21-2008 at 05:40 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 04-18-2008, 06:38 AM
sureshbabu sureshbabu is offline
D-Web Programmer
 
Join Date: Jul 2007
Location: India
Posts: 98
sureshbabu is on a distinguished road
Send a message via AIM to sureshbabu Send a message via MSN to sureshbabu Send a message via Yahoo to sureshbabu Send a message via Skype™ to sureshbabu
Default Re: PHP Classes For Beginners

what about member functions,how could we integrate the member functions with box object?
__________________
Thanks
Regards
Sureshbabu Harikrishnan
+91 9884320017
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 04-18-2008, 06:45 AM
jegan jegan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 161
jegan is on a distinguished road
Default Re: PHP Classes For Beginners

Member Function in BOX

The behavior of an object is defined by the member functions. The behavior or the member functions is normally affects the state or member variables. The box objects can have a member function to initialize the member variables width and height.

For example
PHP Code:
function pmWidth)
{
     
$this->width    =    pmWidth;
}
function 
setHeight(pmHeight)
{
    
$this->height    =    pmHeight;

Class
PHP Code:
class Box
{
    var  
width;
    var 
height;

function  
setWidth(pmWidth)
{
     
$this->width    =    pmWidth;
}
function  
setHeight(pmHeight)
{
    
$this->height    =    pmHeight;
}


__________________
Thanks & Regards,
Jegan CBK
"We will either find a way, or make one!”

Last edited by jegan : 04-21-2008 at 05:39 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 04-21-2008, 05:30 AM
saravanan saravanan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 181
saravanan is on a distinguished road
Default Re: PHP Classes For Beginners

how to create objects using th class?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 04-21-2008, 05:32 AM
jegan jegan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 161
jegan is on a distinguished road
Default Re: PHP Classes For Beginners

hi saravanan here i have writen a simple program to create objects

The Box object to calculate the area of box
PHP Code:
class Box
{
    var  
width;
    var 
height;

function  
setWidth(pmWidth)
{
     
$this->width    =    pmWidth;
}
function  
setHeight(pmHeight)
{
    
$this->height    =    pmHeight;
}
//This function now added to calculate the area of box object
function area()
{
    return 
$this->width*$this->height;
}


}

$object        =    new Box;    //The new key word used to create an object
$object->setWidth(100);
$object->setHeight(100);
print(
"Area of object box");
$object->area(); 
output

Area of object box :1000
__________________
Thanks & Regards,
Jegan CBK
"We will either find a way, or make one!”
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #11 (permalink)  
Old 04-21-2008, 05:36 AM
saravanan saravanan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 181
saravanan is on a distinguished road
Default Re: PHP Classes For Beginners

how many objects can be created using a class? is there any restriction to create objects?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #12 (permalink)  
Old 04-21-2008, 05:38 AM
jegan jegan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 161
jegan is on a distinguished road
Default Re: PHP Classes For Beginners

we can create n number of objects using a class. i think there is no restrictions in numbers to create objects. but we can restrict by the access level. we will see all access levels supported by php using simple examples.
__________________
Thanks & Regards,
Jegan CBK
"We will either find a way, or make one!”
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #13 (permalink)  
Old 04-22-2008, 04:45 AM
saravanan saravanan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 181
saravanan is on a distinguished road
Default Re: PHP Classes For Beginners

What is constructor?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #14 (permalink)  
Old 04-22-2008, 04:47 AM
jegan jegan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 161
jegan is on a distinguished road
Default Re: PHP Classes For Beginners

Constructors
Constructors are functions in a class that are automatically called when you create a new instance of a class with new. A function becomes a constructor, when it has the same name as the class. If a class has no constructor, the constructor of the base class will be called, if it exists.

Point to remember
Constructors are just a function.
Constructor must have the name of the class.
Constructor cannot have any return types
Constructor can be called during the objects creations with new keyword.
__________________
Thanks & Regards,
Jegan CBK
"We will either find a way, or make one!”
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #15 (permalink)  
Old 04-22-2008, 04:50 AM
saravanan saravanan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 181
saravanan is on a distinguished road
Default Re: PHP Classes For Beginners

Difference between constructor functions and member functions?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #16 (permalink)  
Old 04-22-2008, 04:51 AM
jegan jegan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 161
jegan is on a distinguished road
Default Re: PHP Classes For Beginners

1.Constructor function called automatically at time of object creation with new keyword. But member function called only through object of class.

2. Constructor can’t return any value. But member function may be (or) may not be return value. It is depends upon user logic.
__________________
Thanks & Regards,
Jegan CBK
"We will either find a way, or make one!”
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #17 (permalink)  
Old 04-22-2008, 04:52 AM
saravanan saravanan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 181
saravanan is on a distinguished road
Default Re: PHP Classes For Beginners

What is destructor?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #18 (permalink)  
Old 04-22-2008, 04:53 AM
jegan jegan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 161
jegan is on a distinguished road
Default Re: PHP Classes For Beginners

Destructor
A Destructor is a special class member function (method) that has the same name as the class, preceded by (tilde), and is executed when an object is destroyed.

Usually used to deallocate any dynamically allocated memory associated with that object.

A destructor is a method which is automatically invoked when the object is destroyed.

Points to remember

1. Destructor's are invoked automatically, and cannot be invoked explicitly.
2. Destructor's cannot be overloaded. Thus, a class can have, at most, one destructor.
3. Destructor's cannot be used with struc. They are only used with classes.
4. Destructor can’t have any return type.
__________________
Thanks & Regards,
Jegan CBK
"We will either find a way, or make one!”
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #19 (permalink)  
Old 04-23-2008, 06:22 AM
saravanan saravanan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 181
saravanan is on a distinguished road
Default Re: PHP Classes For Beginners

how to use constructor in programs?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #20 (permalink)  
Old 04-23-2008, 06:24 AM
saravanan saravanan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 181
saravanan is on a distinguished road
Default Re: PHP Classes For Beginners

how to use destructor in php program?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
QTP Beginners vadivelanvaidyanathan Testing Tools 13 02-04-2008 05:14 AM
Abstract classes ragavraj PHP Programming 3 12-07-2007 06:55 AM
What are wrapped classes vadivelanvaidyanathan Java Programming 1 07-16-2007 10:24 PM
SEO for beginners spid4r Search Engine Optimization 2 03-11-2007 05:34 AM
PHP and MySQL tutorials for beginners spid4r PHP Programming 0 03-08-2007 09:56 PM


All times are GMT -7. The time now is 02:04 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.