Welcome Guest, Not a member yet? Register   Sign In
Multiple Class Instances vs Function Call
#1

I have been pondering the wisdom of my methodology for a while and thought let's hear what the community has to say. I started this methodology using CI 3 and have now ported the principle to my CI4 applications
Scenario:
Developing a CRM type system and when designing the layout of the User Profile page I have to display various sections containing information from different parts of the system e.g. On the transactions tab we have two columns (1) table of all Invoices (2) table of Orders. Tab Support: Shows Tickets. Tab Technical: shows tables of Login History, Emails from the system etc.
Method 1:
At the start of the view create instances of all the Models that will be used to obtain the data e.g. InvoicesModel, OrderModel, TicketsModel etc. Then call the methods needed as and when required. E.g
Code:
<div>
<?= $modelInvoices->displayTable( $clientId); ?>
</div>
Rinse and repeat for all the information blocks. All the variables gets released once the view has been processed.

Method 2 (which I have been using):
I have created a helper function file which is loaded in BaseController. In this Wrapper Helper I have defined numerous functions that all they do is get an instance of a class and execute a method and return the result. To use the example above i Wrapper Helper:
PHP Code:
function getInvoiceTable$clientId) {
$model = new \App\Models\InvoicesModel();
return 
$model->displayInvoices$clientId);

In the view the code would look something like:
Code:
<div>
<?= getInvoiceTable( $clientId ) ?>
</div>

My thinking was that using the function call the model instance is created, used and the memory released whereas with method one the memory for all the class instances are only released on completion of the view.

The reason that I brought this up that in one app the profile view is now opening 7 model classes and that could increase going forward.

All thoughts and input will be appreciated.
Dirk B.
Abatrans Software
No SEO spam - see forum guidelines
Reply


Messages In This Thread
Multiple Class Instances vs Function Call - by abatrans - 11-27-2020, 10:02 AM



Theme © iAndrew 2016 - Forum software by © MyBB