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 ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| 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. |
| Sponsored Links |
| |||
| 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. |
| |||
| 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! |
| |||
| 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! |
| |||
| 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:
__________________ Thanks & Regards, Jegan CBK "We will either find a way, or make one! Last edited by jegan : 04-21-2008 at 05:40 AM. |
| |||
| 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: PHP Code:
__________________ Thanks & Regards, Jegan CBK "We will either find a way, or make one! Last edited by jegan : 04-21-2008 at 05:39 AM. |
| |||
| hi saravanan here i have writen a simple program to create objects The Box object to calculate the area of box PHP Code: Area of object box :1000
__________________ Thanks & Regards, Jegan CBK "We will either find a way, or make one! |
| |||
| 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! |
| |||
| 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! |
| |||
| 1.Constructor function called automatically at time of object creation with new keyword. But member function called only through object of class. 2. Constructor cant 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! |
| |||
| 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 cant have any return type.
__________________ Thanks & Regards, Jegan CBK "We will either find a way, or make one! |
![]() |
| Thread Tools | |
| Display Modes | |
| |
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 |