Save a class instance

Asked

Viewed 43 times

2

I’m new to programming, but I have a problem I can’t seem to solve.

How do I save an instance of a class in PHP? Because currently I instate a class and after the script stops running, everything I assign is lost. And I have to instantiate again.

Class example:

<?php

     class Usuario{
        private $nome;
        private $idade;
        // ... outros atributos

        public function CalcularSalario(){
           //Realizar calculo complexo com base nas informações salvar na classe
        }

     }

So every time I need to fetch user data in the bank and perform a "complex" salary calculation. But if I already had it somewhere, I would just retrieve the class and if there is any change in the user, I would just reset the values and have it calculated again.

  • 2

    Depends on what you want to do. You should probably have a table with this data that is always calculated, or modify the existing table to keep it there.

  • @Maniero I’ll give you an example, Salary Base 3,000, I have to get INSS, IR, if I have aux. education, adds half the value that the spent user of education, if you have aux. medico, charges a fee that depends on age is more expensive. etc. You think I should have it on the table?

  • 1

    I don’t know, it prevents the calculation, it depends on your need for architecture. Of course, if this is the complex calculation, I don’t know if I should. Alias if the concern is PHP performance is the wrong language. There is no way I can tell what to do without understanding the problem altogether.

  • @Maniero Coordinator enters the system, selects a user and can add/remove benefits, increase salaries, change jobs etc. My idea was that only the first time brought everything from the database and instantiated in a class. When you save what you need, you retrieve the class and change the values that were changed on the screen. Recalculate and save to the database. So I wouldn’t need to recover everything again every change on screen.

  • Since the system is web, I don’t know if that would be possible.

  • 1

    You’re talking about the workflow, not architecture, this is an architectural decision. Anyway, really web is not the ideal to do this. Almost always the option for the web is wrong, but picked up fashion... Web you are stateless, is not suitable for this type of application, and the bulk of applications require statefullness, Most people don’t understand that. So for me the solution is to calculate again every time or keep a table with the data. Of course it is possible to use some intermediate form, as a DB style Nosql, but I think complicated the below for little gain, and more...

  • 1

    ... to fix the mistake made by deciding to do something for the web that shouldn’t be.

  • 3

    "currently I instate a class and after the script stops running, everything I assign is lost. And I have to instantiate again." - That conclusion is a pearl. It’s already better than most "PHP" programmers, who don’t understand this, and you have every reason to have identified the problem, because it’s real. This is one of the biggest reasons OO in PHP is in most cases a big waste of time. It loses almost all OO sense in a stateless application.

  • 1

    @Bacco well said, OOP is about managing the state, in an application that loses the state serves several less object-oriented approaches. I’ve given up hope that people will learn that.

Show 4 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.