D
D
Dzhemchik2014-09-15 19:34:06
PHP
Dzhemchik, 2014-09-15 19:34:06

What is the best way to override parent class method?

Good day.
The other day I tried to write my own "engine", and when writing classes, I ran into the following problem.
Strict standards: Declaration of User::isExists() should be compatible with that of GlobalClass::isExists() in С:\...\mytest\lib\user_class.php on line 31
The parent and child class names are absolutely correct. The only thing I read is that you need to somehow additionally override the method, that is, the one I wrote

public function __construct($db) {
    parent::__construct("users", $db);
  }

not enough.
Here is the code in more detail:
require_once "global_class.php";

class User extends GlobalClass {

  public function __construct($db) {
    parent::__construct("users", $db);
  }
public function isExists($login) {
    return $this->isExists("login", $login);
  }

and its parent:
abstract class GlobalClass {
  private $db;
  private $table_name;
  protected $config;
  protected $valid;
  
  protected function __construct($table_name, $db) {
    $this->db=$db;
    $this->table_name=$table_name;
    $this->config=new Config();
    $this->valid=new CheckValid();
  }
....
protected function isExists($field, $value) {
    return $this->db->isExists($this->table_name, $field, $value);
  }

I would be very grateful for your help .. otherwise I could not fall asleep today. And yes, I have already gone to study OOP in more detail.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2014-09-15
@dzhem911

What exactly do you not understand in the phrase
It can be interpreted as
the definition of the User::isExists() method must not differ from GlobalClass::isExists()
that is, you break the interface, your method in the class of the heir obviously does something that is not what is required of it.
Learn the principles of OOP and GRASP (you can use video lectures, here you need someone to explain).

V
vdem, 2014-09-15
@vdem

In a nutshell: the declaration of the isExists() method must match the declaration of this method in the ancestor class, which is what the interpreter told you. Your settings don't match.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question