K
K
karpo5182020-04-15 19:25:03
PHP
karpo518, 2020-04-15 19:25:03

How to pass a class object as a function argument inside its private method?

I created a CRUD model for a database table in the form of Active Record. When validating and saving some fields in the model, additional operations are performed. The class is big enough. To make the architecture of the code more understandable, I divided some of the fields into groups and moved their logic into separate submodels, but left the fields themselves in the main model. I was going to run the validate_all() class, and already call submodel1->validate(), submodel2->validate() in it.

But! Subclasses need access to the main model object in order to store validation errors in the main model, and to overwrite fields in the main model. But I can't pass the value of $this to the function. For example, I call the validate_all() function of the main class, and in it I need to call the child class's validate_meta function. And this method must have access to an instance of the parent class. How to organize it?

I must say right away that I really would not want to split the table into subtables. There must be a pattern that allows a subclass to access the class. So far, only sending a reference to an object from outside comes to mind:

$item = new Item();
$item->obj = $item;


It's inconvenient and I'm not at all sure it will work.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
John Didact, 2020-04-15
@karpo518

I didn’t understand much ... But, if you need to use, so to speak, yourself in yourself, a class object in the method of this object, then $this, I think, is the most, if I understand correctly.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question