I am struck at overloading the parent's class methods from an inherited child at level2.
abstract class parent
-> child1 extends parent
-> final class child2 extends child1
I want to overload the methods of parent in child2
abstract class Shape
{
protected $length;
protected $height;
protected $a;
protected $b;
protected $c;
public function getCoordinates($length,$height)
{
$this->length=$length;
$this->height=$height;
}
public function getSides($a,$b,$c)
{
$this->a=$a;
$this->b=$b;
$this->c=$c;
}
abstract public function area();
abstract public function perimeter();
abstract public function display();
}
class rectangle extends Shape
{
public function area()
{
return round(($this->length)*($this->height),2);
}
public function perimeter()
{
return round(2*(($this->a)+($this->b)),2);
}
public function display()
{
echo "area is :". rectangle::area() . "<br>";
echo "perimeter is : ". rectangle::perimeter() ."<br>";
}
}
final class triangle extends rectangle
{
function __call($method_name, $arguments) // this is wrong ........please modify here to call area(),which is in shape class();
{
$accepted_methods = array("getCoordinates","area","perimeter");
}
public function area()
{
return round((($this->length)*($this->height)*($this->width)/2),2);
}
public function perimeter()
{
return round((($this->a)+($this->b)+($this->c)),2);
}
public function getCoordinates($length,$height,$width)
{
$this->length=$length;
$this->height=$height;
$this->width=$width;
}
public function display()
{
echo "area is :". triangle::area() . "<br>";
echo "perimeter is : ". triangle::perimeter() ."<br>";
}
}
$r=new rectangle();
$r->getCoordinates(1,2,4);
$r->getSides(6,2);
$r->display();
$ot = new triangle();
$ot->getCoordinates(1,2,4);
$ot->getSides(6,2,3);
$ot->display();
?>
Thanks in advance
$r->getSides(6,2);
Your abstract class demands three arguments! Plus the function is actually a setter method. You should name it setSides();. Same with getCoordinates().
Update: I think you are confusing inheritance with overloading. Here is an example for overloading with __call. I assume that's not what you are trying to do but what you have in your example. Maybe this will help.
abstract class overloadTestAbstract {
public function printOne($show) {
echo __METHOD__ . ': ' . $show . '<br />';
}
}
class overloadTestOne extends overloadTestAbstract {
public function __call($method,$arguments) {
$methods = array('printOne','printTwo','printThree');
if ( in_array($method,$methods) ) {
echo __METHOD__ . ' :OVERLOAD METHOD: ' . $arguments[0] . '<br />';
} else {
echo 'We are so sorry, but this method is available';
}
}
public function printTwo($show) {
echo __METHOD__ . ': ' . $show . '<br />';
}
}
Then if you do this:
$test = new overloadTestOne();
$test->printOne('Hello World');
$test->printTwo('Goodbye World');
$test->printThree('Hello World, again');
$test->printFour('Goodbye World, again');
you will get this
// print results
'overloadTestAbstract::printOne: Hello World'
'overloadTestOne::printTwo: Goodbye World'
'overloadTestOne::__call :OVERLOAD METHOD: Hello World, again'
'We are so sorry, but this method is available'
Although I have printOne and printTwo in the overload __call as accepted methods they are not used because these methods are already defined, they are handled by the existing methods as expected. On the other hand printThree gets overloaded because the method does not exist. Same with printFour but that method has no intend to print the argument. The array you have defined with the accepted methods doesn't do a thing. It is just an array. You have to assign some task to these methods or return some error like I did.
Related
I am trying to use a display function in my extened class that first fetches a display function in the parent class. It however does not display the variable in the echo statement. The gameType (In this case "One-day") does not display.
<?php
class Cricket
{
protected $gameType;
function __construct($gameType)
{
$this->gameType=$gameType;
}
function display()
{
echo 'The cricket match is a ' . $this->gameType . " match";
}
}
class Bowler extends Cricket
{
public $type;
public $number;
function __construct($type,$number)
{
$this->type=$type;
$this->number=$number;
parent::__construct($this->gameType);
}
function display()
{
parent:: display();
echo " with " . $this->number . " " . $this->type . " bowler";
}
}
$one = new Cricket("day-night");
$one->display();
echo'<br>';
$two = new Cricket("day-night");
$two = new Bowler("left-hand","2");
$two->display();
?>
The process of instantiating your Bowler class will in fact, as is implied by calling the parents constructor parent::__construct();, create a brand new Cricket class as well as the Bowler class.
So attempting to access a property of this newly created Cricket class makes no sense.
So when you instantiate the Bowler class you will also have to pass any data that the Cricket class requires for it's successful construction.
So for example
<?php
class Cricket
{
protected $gameType;
function __construct($gameType)
{
$this->gameType=$gameType;
}
function display()
{
echo 'The cricket match is a ' . $this->gameType . " match";
}
}
class Bowler extends Cricket
{
public $type;
public $number;
function __construct($gameType, $type, $number)
{
$this->type=$type;
$this->number=$number;
parent::__construct($gameType);
}
function display()
{
parent:: display();
echo " with " . $this->number . " " . $this->type . " bowler";
}
}
$two = new Bowler('day-night', "left-hand","2");
$two->display();
I'm trying to set the "setFlyBhavior(FlyBehavior $newFlyBehavior)" dynamically. Can anyone explain why it will not work in the code below? I appreciate your help.
<?php
abstract class Duck {
public $flyBehavior;
public function performFly() {
return $this->flyBehavior->fly();
}
public function setFlyBhavior(FlyBehavior $newFlyBehavior) {
$this->flyBehavior = $newFlyBehavior;
}
}
interface FlyBehavior {
public function fly();
}
class GotWings implements FlyBehavior {
public function fly(){
return "<br />I'm flying with wings!!<br />";
}
}
class NotWings implements FlyBehavior {
public function fly(){
return "<br />I can't fly I have no wings!!<br />";
}
}
class FlyingDuck extends Duck {
public function __construct(){
$this->flyBehavior = new GotWings();
}
}
$ducky = new FlyingDuck();
// Code works when the setFlyBehavior function is commented out.
$ducky->setFlyBehavior(new NoWings);
echo "<br />I'm a Duck: " . $ducky->performFly();
?>
Note: The code works when not calling the "ducky->setFlyBehavior function. I also tried defining the setFlyBehavior function in the Duck class without using type casting, which also failed e.g.
public function setFlyBhavior($newFlyBehavior) {
$this->flyBehavior = $newFlyBehavior;
}
You are calling the wrong function and class names.
Replace the line:
$ducky->setFlyBehavior(new NoWings);
With:
$ducky->setFlyBhavior(new NotWings);
I have this Base class:
class Base
{
public $extA;
public $extB;
function __construct()
{
}
public function Init()
{
$this->extA = new ExtA();
$this->extB = new ExtB( $this );
}
public function Test()
{
return 'Base Test Here!';
}
}
class ExtA extending the Base Class
class ExtA extends Base
{
public function Test()
{
return 'ExtA Test Here!';
}
}
class ExtB extending the Base Class too
class ExtB extends Base
{
private $base;
public function __construct( $base )
{
$this->base = $base;
}
public function Test()
{
return 'ExtB calling ExtA->Test()::' . $this->base->extA->Test();
}
}
$base = new Base();
$base->Init();
var_dump( $base->Test() );
var_dump( $base->extA->Test() );
var_dump( $base->extB->Test() );
I try to call the ExtA class Test() function from the ExtB,
both of ExtA and ExtB is exnteding the Base class.
My question is : is this ok, or have a better, faster solution for this?
The extends is necessary too?
Or simply enough like this
class ExtA
{
...
}
class ExtB
{
...
}
Thanks!
This is weird way of OOP.
The Base class should not know anything about its children so we shall go more correct way. Let's implement Decorator pattern:
interface IExt
{
public function test();
}
abstract class ExtDecorator implements IExt
{
protected $instance;
public function __construct(IExt $ext)
{
$this->instance = $ext;
}
}
class ExtA extends ExtDecorator
{
public function test()
{
return 'ExtA::test here and calling... ' . $this->instance->test();
}
}
class ExtB extends ExtDecorator
{
public function test()
{
return 'ExtB::test is here and calling... ' . $this->instance->test();
}
}
class Base implements IExt
{
public function test()
{
return 'Base::test here!';
}
}
class Printer
{
public static function doMagic(IExt $ext)
{
echo $ext->test()."\n";
}
}
Printer::doMagic($base = new Base);
// Base::test here!
Printer::doMagic($extA = new ExtA($base));
// ExtA::test here and calling... Base::test here!
Printer::doMagic(new ExtB($extA));
// ExtB::test is here and calling... ExtA::test here and calling... Base::test here!
You can play further any way you want
I have the following class and I want to implement chaining methods. I am kinda teaching my ownself so I thought it would be neat to test chaining. However that didnt work. What would I need to do that
echo $animal->name.' says'.$animal->speak()->likes()."<br />";
here is my complete code
<?php
class Animal{
var $name;
function __construct(){
$this->name = $name;
}
}
class Dog extends Animal{
public function speak(){
return "Woof Woof";
}
public function likes(){
return "steaks";
}
}
class Cat extends Animal{
public function speak(){
return "Meow Meow";
}
public function likes(){
return "tuna";
}
}
$animals = array(new Dog('skippy'), new Cat('snowball'));
foreach($animals as $animal){
echo $animal->name.' says'.$animal->speak()->likes()."<br />";
}
?>
<?php
class Animal {
function speak() {
echo "Random Noise!\n";
return $this;
}
}
class Dog extends Animal {
function bark() {
echo "bark!\n";
return $this;
}
}
$a = new Dog();
$a->speak()->bark();
You need to return $this in order to chain your methods.
If you want to chain method, you need to return $this in the method for chaining.
try to write instead of
echo $animal->name.' says'.$animal->speak()->likes()."<br />";
that :
printf('%s says %s %s',#animal->name,$animal->speak()->likes());
this is my class:
class toyota extends car {
function drive() {
}
function break() {
}
}
class car {
function pre() {
}
}
Is there any way I can do so that when I run $car->drive(), $car->break() (or any other function in toyota), it would call $car->pre() first before calling the functions in toyota?
Yep. You could use protected and some __call magic:
class toyota extends car {
protected function drive() {
echo "drive\n";
}
protected function dobreak() {
echo "break\n";
}
}
class car {
public function __call($name, $args)
{
if (method_exists($this, $name)) {
$this->pre();
return call_user_func_array(array($this, $name), $args);
}
}
function pre() {
echo "pre\n";
}
}
$car = new toyota();
$car->drive();
$car->dobreak();
http://ideone.com/SGi1g
You could do the following, but I don't think that is what you want.
class toyota extends car {
function drive() {
$this->pre();
}
function break() {
$this->pre();
}
}
class car {
function pre() {
}
}
You may want to look into PHP specific magic methods. http://php.net/manual/en/language.oop5.magic.php
This will better done with the magic methods called __call()
public function __call($name, $arguments)
{
$this -> pre();
return $this -> $name($arguments);
}
What is this method? It overrides the default method call, so that preCall State can be invoked.
Your toyota class
class toyota extends car {
public function __call($name, $arguments)
{
$this -> pre();
return call_user_func_array(array($this, $name), $arguments);
}
function drive() {
}
function break() {
}
}
If you are using PHP5 (>=5.3.2), there is a solution that works with declaring all methods as private. This will enforce method call from single function call:
exec_method()
To run at: http://ideone.com/cvfCXm
The code snippet is here:
<?php
class car {
//method to get class method
public function get_method($method_name) {
$class = new ReflectionClass(get_class($this));
$method = $class->getMethod($method_name);
$method->setAccessible(true);
return $method;
}
public function exec_method($method_name, $arg_args=array()) {
//execute the pre() function before the specified method
$this->pre();
//execute the specified method
$this->get_method($method_name)->invokeArgs($this, $arg_args);
}
public function pre() {
echo 'pre';
echo '<br />';
}
}
class toyota extends car {
private function drive() {
echo 'drive';
echo '<br />';
}
private function brake() {
echo 'brake';
echo '<br />';
}
}
$toyota = new toyota();
$toyota->exec_method('drive');
$toyota->exec_method('brake');
?>
Reference:
Answer to Best practices to test protected methods with PHPUnit [closed]
Just add a constructor, like this...
class toyota extends car {
function __construct() {
$this->pre();
}
function drive() {
echo "drive!";
}
function dobreak() {
echo "break!";
}
}
class car {
function pre() {
echo "Hello!";
}
}
$car = new toyota();
$car->drive();
$car->dobreak();
Classes which have a constructor method call this method on each
newly-created object, so it is suitable for any initialization that
the object may need before it is used.
break is reserved, so you shouldn't use this as a function name.