S
S
Svyatoslav Nemato2016-10-12 12:34:19
PHP
Svyatoslav Nemato, 2016-10-12 12:34:19

How to call an ancestor class variable?

There are two classes a and b how to get variables from class b if they are reassigned?

class a {
  public $data;
}

class b extends a {
  public $data;
  public function get_data() {
    Как вызвать $data класса a?
  }
}

This is how it doesn't work
class a {
  public $data;
}

class b extends a {
  public $data;
  public function get_data() {
    parent::data;
  }
}

Found a crutch on the Internet
class a {
  public $data;
  public function data() {
    return $this->data;
  }
}

class b extends a {
  public $data;
  public function get_data() {
    parent::data();
  }
}

Is there a solution without a crutch?
[ Solution ]
Use different variable names, unfortunately there is no normal variable redirection in puff yet.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
romy4, 2016-10-12
@makklovskiy

you violated the basic principle of DRY (do not repeat yourself). this means that you don't need to define a variable with the same name and access in the child class if it exists in the parent(s)

A
Alexander Aksentiev, 2016-10-12
@Sanasol

parent::data;
Why are you calling like that? It's not a constant.
parent::$data;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question