M
M
Max Ba2018-10-25 14:28:11
PHP
Max Ba, 2018-10-25 14:28:11

How to add a method to an existing class?

Guys tell me. You need to add a new method to the class to extend the functionality. It would be possible to create a class heir, but then you have to create a second object, like

$obj1 = new();
$obj1->method1();

$obj2 = new();
$obj2->method2(); //чтобы выполнить новый метод

And I want to be able to do this:
$obj1->method2();
Is it easier to add a couple of methods to an already working class?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sanan :o, 2018-10-25
@sanantech

You can do this for example:

$foo = foo.getInstance();
$foo->first();
$foo->last();

and here is foo itself:
<?php
class foo {
public function first() {
// что то тут
}
public function last() {
// что то тут
}
public static function getInstance() {
return self;
}
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question