This is a discussion on PHP Classes For Beginners within the PHP Programming forums, part of the Web Development category; eacy to use constructors in php class. when we creating a objects from using the new keyword the constructor function ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| eacy to use constructors in php class. when we creating a objects from using the new keyword the constructor function automatically executes PHP Code: Area of object box :1000
__________________ Thanks & Regards, Jegan CBK "We will either find a way, or make one!” |
| Sponsored Links |
| |||
| Using destructors PHP Code:
__________________ Thanks & Regards, Jegan CBK "We will either find a way, or make one!” |
| |||
| What is inheritance? Probably the greatest feature of the PHP OOP model is Inheritance. Inheritance is the ability of PHP to extend classes (child classes) that inherit the characteristics of the parent class. The resulting object of an extend class has all the characteristics of the parent class, plus whatever is in the new child, or extended class. In this instance we will extend the vehicle class and add a motorcycle. A motorcycle is still a vehicle and shares many of the same attributes such as price, drive etc. But a motorcycle has some unique features that a car does not.
__________________ Thanks & Regards, Jegan CBK "We will either find a way, or make one!” |
| |||
| Ans I have written a program that inherits our Box class. Here the base class is Box and the derived class is BoxChild. We have a scenario to find the weight of a box. So we need to write a new class with width, height and weigh member variables. But this will lead to us to do lot of workload. But we can inherit our Box object and we can add required thinks in child class. PHP Code:
__________________ Thanks & Regards, Jegan CBK "We will either find a way, or make one!” |
| |||
| 1. Restricting access to class properties (variable). 2. Restricting access to methods. 3. They support the concept of encapsulation, which promotes the idea of hiding functionality. 4. Access modifiers allow you to define who does or doesn’t have access to certain features in class
__________________ Thanks & Regards, Jegan CBK "We will either find a way, or make one!” |
| |||
| Access Modifiers 1. public 2. private 3. protected 4. Final
__________________ Thanks & Regards, Jegan CBK "We will either find a way, or make one!” Last edited by jegan : 04-28-2008 at 06:00 AM. |
| |||
| Public variables and functions are accessible from anywhere in your script. We can see in the below example that the public variable $num is set from user space and we call a public method that adds two the number and returns the value of $num+2. Having our properties (variables) visible or accessible from any part of our script works in our favors here, but it can also work against us. A could arise if we lost track of our values and changed the value of $num. To counter this problem we can create a method to set the value for us. Even with this in place it is still possible for somebody to simply access the $num variable. So we make the variable private. This ensures us that the property is only available within the class itself. It is private to the calling class. PHP Code:
__________________ Thanks & Regards, Jegan CBK "We will either find a way, or make one!” |
| |||
| Using Protected Variables and functions marked as protected are accessible only through the object that owns them, whether or not they are declared in that object's class or whether they have descended from a parent class. Consider the following code: PHP Code: However, note that the bark() function calls the getName() function, which is part of the dog class and was marked private - this will stop the script from working, because private variables and functions cannot be accessed from inherited classes. That is, we cannot access private dog functions and variables from inside the poodle class. Now try changing bark() to protected, and all should become clear - the variable is still not available to the world as a whole, but handles inheritance as you would expect, which means that we can access getName() from inside poodle.
__________________ Thanks & Regards, Jegan CBK "We will either find a way, or make one!” |
| |||
| Private Variables Private variable and method accessible only within the class. Not possible from outside of class. PHP Code:
__________________ Thanks & Regards, Jegan CBK "We will either find a way, or make one!” |
| |||
| inheritance Probably the greatest feature of the PHP OOP model is Inheritance. Inheritance is the ability of PHP to extend classes (child classes) that inherit the characteristics of the parent class. The resulting object of an extend class has all the characteristics of the parent class, plus whatever is in the new child, or extended class. The most common is simplifying and reducing instances of redundant code that is code reusability.
__________________ Thanks & Regards, Jegan CBK "We will either find a way, or make one!” |
| |||
| Type of Inheritance 1.Single inheritance 2.Multi-level inheritance 3.Multiple inheritance can’t achieve in php.
__________________ Thanks & Regards, Jegan CBK "We will either find a way, or make one!” |