0

I am working on an OOP implementation and I have the following:

abstract class Parent{
   public abstract function select($order="desc");

}


class Child extends Parent{
   public function select($order) // here is the problem error
   {
      // selection code 
   }

}

This throws an error that tells me the declaration must be compatible with the parent method.

I did implement it with the right parameters except I didn't carry over the default parameter setting.

I do not want to copy past the same prototype of parent method in 100 classes if i want someday change the default value. How can I do this?

does generic exist in php ??

1
  • I think you ought to make the implementation header of your function match the abstract function. Commented Oct 8, 2013 at 0:13

4 Answers 4

3

public abstract function select($order="desc"); and public function select($order) dont match. remove the default value from the abstract function.

Sign up to request clarification or add additional context in comments.

2 Comments

i need a commune default value between all who implement this method what you are saying does not resolve anything
Store it on the object. Don't pass it in as an arg. If you have to have it as an Arg, have it as an optional arg. Initialize it in the constructor. And if you need it to be different in the lower level classes, override it on the object.
1

About the only way I can see to avoid updating a lot of values if you ever want to change the common default is this:

abstract class Parent{
   const DEFAULT_SELECT_ORDER = "desc";

   public abstract function select($order = "");

   protected static function select_order(&$order) 
   {
       if (empty($order) || !in_array(strtolower($order), array("asc", "desc"))) {
           // additional test to check if the value is valid
           $order = self::DEFAULT_SELECT_ORDER;
       }
   }    
}


class Child extends Parent{
   public function select($order = "") // here is the problem error
   {
      self::select_order($order);

      // selection code 
   }
}

Hmm - another, probably better approach:

abstract class Parent {
    protected $order = "desc";

    public function order($order) {
        if (in_array(strtolower($order), array("asc", "desc"))) {
            $this->order = $order;
        } else {
            // probably should throw an exception or return false or something
        }

        return true;
    }

    public abstract function select();
}



class Child extends Parent {
    public function select() {
        // select code using $this->order
    }
}



$query = new Child();
$query->order("asc");
$results = $query->select();

Comments

0

All you need to do is change your extended method to be something like:

public function select($order="some other value") // here is the problem error
   {
      // selection code 
   }

Essentially because the original method has a default value then ALL overrides must have a default value.

In order to do what you want you would have to make $order an object property of Parent and change the method signature to get rid of the $order parameter. Then within any specific implementation you could simply set $order to whatever else you want.

3 Comments

yea but i want a commune default value between all of them and if i want to change it it will be easy
Can you define what a "commune" default value is? Is that like a hippie value that tries to sell you flowers on the side of the road? /sarcasm: Either way, this is how it works.
lol it means all classes which inherits from this parent class i already know the default value is xxx you see but instead if each must has the prototype like it parent but not the value for example someone else can implement with yyyy like default value
0

You possible could use my tiny library ValueResolver, for example:

$myVar = ValueResolver::resolve($var, $default);

and don't forget to use namespace use LapaLabs\ValueResolver\Resolver\ValueResolver;

There are also ability to typecasting, for example if your variable's value should be integer, so use this:

$id = ValueResolver::toInteger('6 apples', 1); // returns 6
$id = ValueResolver::toInteger('There are no apples', 1); // returns 1 (used default value)

Check the docs for more examples

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.