This is a discussion on PHP Classes For Beginners within the PHP Programming forums, part of the Web Development category; give simple example for interface in php?...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| <?php // Declare the interface 'iTemplate' interface iTemplate { public function setVariable($name, $var); public function getHtml($template); } // Implement the interface // This will work class Template implements iTemplate { private $vars = array(); public function setVariable($name, $var) { $this->vars[$name] = $var; } public function getHtml($template) { foreach($this->vars as $name => $value) { $template = str_replace('{' . $name . '}', $value, $template); } return $template; } } // This will not work // Fatal error: Class BadTemplate contains 1 abstract methods // and must therefore be declared abstract (iTemplate::getHtml) class BadTemplate implements iTemplate { private $vars = array(); public function setVariable($name, $var) { $this->vars[$name] = $var; } } ?>
__________________ Thanks Regards Sureshbabu Harikrishnan ![]() +91 9884320017 |
| |||
| Reusing the same method name in a class called overloading. The overloading method shares common method name and different return type and different parameter list.
__________________ Thanks & Regards, Jegan CBK "We will either find a way, or make one!” |
| |||
| Both method calls and member accesses can be overloaded via the __call, __get and __set methods. These methods will only be triggered when your object or inherited object doesn't contain the member or method you're trying to access. All overloading methods must not be defined as static. All overloading methods must be defined as public.
__________________ Thanks & Regards, Jegan CBK "We will either find a way, or make one!” |
| |||
| Member overloading void __set ( string $name , mixed $value ) mixed __get ( string $name ) bool __isset ( string $name ) void __unset ( string $name ) Class members can be overloaded to run custom code defined in your class by defining these specially named methods. The $name parameter used is the name of the variable that should be set or retrieved. The __set() method's $value parameter specifies the value that the object should set the $name. Note: The __set() method cannot take arguments by reference. overloading with __get, __set, __isset and __unset example PHP Code: The above example will output: Setting [a] to 100 OK! Getting [a] Returning: 100 Setting [a] to 101 OK! Getting [z] Nothing! Setting [z] to 1 Not OK! Checking if a is set bool(true) Unsetting a Checking if a is set bool(false) bool(true) [/output]
__________________ Thanks & Regards, Jegan CBK "We will either find a way, or make one!” |
| |||
| Method overloading mixed __call ( string $name , array $arguments ) The magic method __call() allows to capture invocation of non existing methods. That way __call() can be used to implement user defined method handling that depends on the name of the actual method being called. This is for instance useful for proxy implementations. The arguments that were passed in the function will be defined as an array in the $arguments parameter. The value returned from the __call() method will be returned to the caller of the method. overloading with __call example PHP Code: Method test called: array(4) { [0]=> int(1) [1]=> string(1) "2" [2]=> float(3.4) [3]=> bool(true) } array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) }
__________________ Thanks & Regards, Jegan CBK "We will either find a way, or make one!” |
| |||
| Overriding Overriding a method means redefining a method inherited from the parent class in the child class.
__________________ Thanks & Regards, Jegan CBK "We will either find a way, or make one!” |
| |||
| A simple example PHP Code:
__________________ Thanks & Regards, Jegan CBK "We will either find a way, or make one!” |
| |||
| Polymorphisms “The occurrence of different forms, stages, or types in individual organisms or in organisms of the same species, independent of sexual variations.” (dictionary.com). By that definition, we could assume polymorphism is a programmatic way to represent the same object through multiple states or stages. That’s great, but probably still unclear to many of us. What it really means is this: programming to an interface or base class without regard to an object’s concrete class.
__________________ Thanks & Regards, Jegan CBK "We will either find a way, or make one!” |
| |||
| For example, consider a class called Person. We can subclass Person with classes called David, Charles, and Alejandro. Person has an abstract method called AcceptFeedback(), which all subclasses implement. That means that any code using any subclasses of the Person base class can call the AcceptFeedback() method with confidence, knowing that the class itself handles logic that would otherwise appear in a conditional. You do not have to check whether the object is a David or an Alejandro, just that it is a Person. The effect is that your code is written to the lowest common denominator – the Person class. The Person class in this example could also be created as an interface. There are some differences, primarily that an interface imparts no behavior, only a set of rules, so to speak. A Person interface would say “you must support the AddFeedback() method” whereas a Person class could provide some default code for the AddFeedback() method, saying “if you choose not to support AddFeedback(), you will be provided with a default implementation.” Choosing interfaces or base classes is the subject for another article entirely, but in general, if you need a default implementation for a method, provide it through a base class. If you are simply outlining a set of expectations for your classes, then use an interface.
__________________ Thanks & Regards, Jegan CBK "We will either find a way, or make one!” |
| |||
| i donot know How can we implement polymorphisms in PHP? plz tell how to use constructor and destructor in programs? |
| |||
| thanks i gain a lots of knowledge from all of you. |
| |||
| This class can be used to implement class destructor functionality in a way that it works under PHP 4. It works as a base class that keeps track of all objects that are created by its subclasses. If a PHP script attempts to exit before an object is destroyed, a shutdown function is called to explicitly destroy any pending objects.
__________________ Test Management |