B
B
bilyaev822021-10-18 19:52:27
OOP
bilyaev82, 2021-10-18 19:52:27

How to call child class in parent method?

Hello.
There is this code:
echo (new Select->setAttr('name', 'list'))->add( (new Option())->setText('item1') )->show()
I don't know how to call the child class Option in the add method as described above.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
montray, 2021-10-18
@quiex

Most likely you don't mean "call the class" but accept it.
I will assume that Option::setText() returns void, and Select::add() takes Select(Option).
You either need to return a reference to the instance in setText() (return $this) or place the text initialization in the Option class constructor and accept it in $select->add(new Option('text')).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question