This is a discussion on PHP Classes For Beginners within the PHP Programming forums, part of the Web Development category; what is inheritance...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| PHP Code:
__________________ Thanks & Regards, Jegan CBK "We will either find a way, or make one!” |
| |||
| PHP Code:
__________________ Thanks & Regards, Jegan CBK "We will either find a way, or make one!” |
| |||
| Multiple inheritance not achieve directly. but multiple inheritance achieved in some other way is trait and use keywords. Trait act like class and use act like extends. PHP Code:
__________________ Thanks & Regards, Jegan CBK "We will either find a way, or make one!” |
| |||
| You may find yourself writing code that refers to variables and functions in base classes. This is particularly true if your derived class is a refinement or specialisation of code in your base class. Instead of using the literal name of the base class in your code, you should be using the special name parent, which refers to the name of your base class as given in the extends declaration of your class. By doing this, you avoid using the name of your base class in more than one place. Should your inheritance tree change during implementation, the change is easily made by simply changing the extends declaration of your class. PHP Code:
__________________ Thanks & Regards, Jegan CBK "We will either find a way, or make one!” |
| |||
| Sometimes it is useful to refer to functions and variables in base classes or to refer to functions in classes that have not yet any instances. The :: operator is being used for this. PHP Code: There are class functions, but there are no class variables. In fact, there is no object at all at the time of the call. Thus, a class function may not use any object variables (but it can use local and global variables), and it may not use $this at all. In the above example, class B redefines the function example(). The original definition in class A is shadowed and no longer available, unless you are referring specifically to the implementation of example() in class A using the ::-operator. Write A::example() to do this (in fact, you should be writing parent::example(), as shown in the next section). In this context, there is a current object and it may have object variables. Thus, when used from WITHIN an object function, you may use $this and object variables.
__________________ Thanks & Regards, Jegan CBK "We will either find a way, or make one!” |
| |||
| Answer for abstract class: An abstract class is a class that cannot (or should not) be instantiated. They are surprisingly useful for certain purposes. 2. In PHP a class is merely a set of statements that perform a specific task, and its definition contains both properties and methods, which act as the blueprint from which to create –- or instantiate -– independent objects. If can’t create objects from a class then what is the use of that classes? You’re developing an application where different classes are organized in a well-structured hierarchy. On top of this hierarchical relationship, you can define an abstract base class, which exposes a given number of generic properties and methods, in order to model the characteristics and behaviors of all its child classes. From a purist point of view, this abstract class shouldn’t provide any explicit definition for its methods or properties. These methods and properties should be specifically defined within the subclasses, which automatically would inherit them from the base class. Indeed, this is a powerful concept that can be very useful when applied in the appropriate development context
__________________ Thanks & Regards, Jegan CBK "We will either find a way, or make one!” |
| |||
| Answer for how to define and use abstract class : PHP 5 introduces abstract classes and methods. It is not allowed to create an instance of a class that has been defined as abstract. Any class that contains at least one abstract method must also be abstract. Methods defined as abstract simply declare the method's signature they cannot define the implementation. When inheriting from an abstract class, all methods marked abstract in the parent's class declaration must be defined by the child; additionally, these methods must be defined with the same (or weaker) visibillity. For example, if the abstract method is defined as protected, the function implementation must be defined as either protected or public. Abstract class example PHP Code: ConcreteClass1 FOO_ConcreteClass1 ConcreteClass2 FOO_ConcreteClass2
__________________ Thanks & Regards, Jegan CBK "We will either find a way, or make one!” |
| |||
| interfaces allow you to create code which specifies which methods a class must implement, without having to define how these methods are handled. Interfaces are defined using the interface keyword, in the same way as a standard class, but without any of the methods having their contents defined. All methods declared in an interface must be public, this is the nature of an interface. implements To implement an interface, the implements operator is used. All methods in the interface must be implemented within a class; failure to do so will result in a fatal error. Classes may implement more than one interface if desired by separating each interface with a comma.
__________________ Thanks & Regards, Jegan CBK "We will either find a way, or make one!” |
| |||
| hi Ur tutorial of PHP based on the object oriented programming is good. You should proceed ahead regarding cookies and sessions.
__________________ web design company Last edited by chetan1 : 05-08-2008 at 03:33 AM. |
| |||
| Object Interfaces Object interfaces allow you to create code which specifies which methods a class must implement, without having to define how these methods are handled. Interfaces are defined using the interface keyword, in the same way as a standard class, but without any of the methods having their contents defined.
__________________ Thanks Regards Sureshbabu Harikrishnan ![]() +91 9884320017 |