A
A
Alexey2017-02-24 02:08:42
PHP
Alexey, 2017-02-24 02:08:42

What to do with repetitive methods?

Hello! The site has about a dozen separate modules: "news", "events", "comments", etc. And each module, respectively, has its own class in which it is generated.
Each of them has a pagination that is generated by a single external paginator() function;
But for each, you need to pass parameters when connecting: "current page", "amount per page", etc.
It turns out that each class has the same methods: setPage() , setOnPage() , getStart() , getLimit()and so on ... In addition to these, there are still the same ones. How to deal with it? on the one hand, you can throw them into a separate class, and on the other hand, it will turn out that the classes depend on each other, because these methods will have to change the properties of the same name in each of the classes.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
E
entermix, 2017-02-24
@entermix

You are probably designing your application incorrectly. Pagination is usually placed in a separate module/class. I recommend to look at popular PHP OOP frameworks.
I also recommend reading: about inheritance in PHP

H
heartdevil, 2017-02-24
@heartdevil

You can do this:
Create an interface

public interface PagedListInterface
{
//..
PageIndex
PageSize
TotalCount
TotalPages
//..
}

then implement the interface
class PagedList implements PagedListInterface
{
//тут вся логика листалки
}

Create another interface
public interface NewsInterface
{
//Различные методы
//И среди прочих
PagedListInterface GetNewsList()
}

And further in the News class
class News implements NewsInterface
{
public PagedListInterface GetNewsList()
}

S
Sama Samsonov, 2017-02-24
@samuser

You need to use inheritance.
Or a separate class must be written for pagination.

M
McBernar, 2017-02-24
@McBernar

Inheritance or traits.
It is not always possible to inherit, but no one bothers you to do traits.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question