0

Recently I have starting trying to do more OOP based programming as I know it's way better than procedural programming but I have a probably simple question. If I want to implement a "tasks" class where each tasks has a title, description, how far completed it is and who submitted it. I have the following code to create the class:

class Tasks
{
    public $title;
    public $description;
    public $completionPercent;
    public $contact;

    public function __construct($title, $description, $completionPercent, $contact)
    {
        $this->title = $title;
        $this->description = $description;
        $this->completionPercent = $completionPercent;
        $this->contact = $contact;
    } 

    public function getTitle()
    {
        return $this->title;
    }

    public function getDescription()
    {
        return $this->description;
    }

    public function getCompletionPercent()
    {
        return $this->completionPercent;
    }

    public function getContact()
    {
        return $this->contact;
    }

    public function setTitle($title)
    {
        $this->title = $title;
    }

    public function setDescription($description)
    {
        $this->description = $description;
    }

    public function setCompletionPercent($completionPercent)
    {
        $this->completionPercent = $completionPercent;
    }

    public function setContact($contact)
    {
        $this->contact =$contact;
    }

}

My question is if I create a tasks and then access it later as in close the browser and then come back to it a few days later when I have an update to it how do I store it and re-access it. I understand databases and use them very often in my current sites I am using to learn but I don't see how storing the information from the class in the database would be logical when I assume you could just skip the class and utilize the database. I read that you could serialize or store XML/json in a file and access the class that way but again I don't see why you would need the class if you could just read a file into an array and use it that way. I know OOP is important but I am still just trying to get it down to where I can start using classes regularly so that I can get familiar with them and progress as a programmer. I am sure there is plenty of reading out on the net about his but I just keep finding stuff on storing class variables in session variables which is not exactly what I want. Let me know if there is something I can clarify, I know when I first make the class I would do something like $task1 = new Task(...); but after closing my browser and logging back in I am not able to still access $task1 correct? I cannot just do $task1->getTitle(); so how do I re-initialize the class?

AndyPet74
  • 659
  • 2
  • 7
  • 24
  • 1
    your questions is not specific at all, I do not quite understand what you need, anyway I would suggest some reading maybe you should take a look at ORM http://stackoverflow.com/questions/1279613/what-is-an-orm-and-where-can-i-learn-more-about-it and then http://www.doctrine-project.org/ or any other implemantation that you can google yourself – scx Jul 14 '16 at 14:03
  • 1
    BTW, your getters and setters are useless. 1) Your properties are `public`, so setters can be circumvented. 2) Your setters don't *do* anything and thus add nothing over direct property access. – Just keep your properties `public` and get rid of the getters/setters. – deceze Jul 14 '16 at 14:10

2 Answers2

2

Classes and objects help you organise your code and logic into self contained units. That is all. And that is very important. It does not address the issue of storage or anything else.

Yes, you would still store the data of the object in the database, and then read again from the database and create an object instance again.

input → new Task(..) → store object data in database
read data from database → new Task(..) → display on website

Why is this better than working with arrays from the database directly? Because it allows you to organise your code better. Even if the object's lifetime is just a fraction of a second, within that fraction all the code that uses the object can be written much more elegantly and most of all type safe, because it is clearly defined what that object looks like. An array is an untyped grab bag of stuff which is hard to work with the more complex your application becomes. Especially refactoring code which uses only arrays is very very hard.

deceze
  • 510,633
  • 85
  • 743
  • 889
1

You are right, you need to store such information somewhere, and right place for that is db. So, question is: why you need to use and create class "Task" if you can directly save it in db. Because you want to build application based on "classes and objects", so not to use procedural programming, as you said. Therefore, you need to use objects and create classes for everything in order to have fully OO application.

So, your application should be based on classes/objects and this is what you and some other developers will work with. Now you just need to save this data from that object into the database. That means, you need introduce some additional layer for this. One very nice is Doctrine ORM. It does mapping and translate your object into db. Usually each class has its own table in db and each field in the class has its own column in db (this is only for classes which need to be saved in db)

The point is, that you can easily use objects in your application like any other objects without knowing how they should be saved in db (or somewhere else). Only what you need to do is:

$task->save();

and that's it. That task will be saved in db.

Marko Krstic
  • 1,417
  • 1
  • 11
  • 13
  • Ok, when I save a class into the database and lets say I have 10 tasks, when I come back to this page after a few days how do I recreate all the Tasks back as objects VIA the data from the database. When I create a tasks like task1 I would just do `$tasks1 = new Tasks(...);` which I get but how do I grab access all those objects again? Back in the class I took when we had to do this we used .get(index) (this was in java) and if we wanted a specific item we had to use some sort of search to find which class matched. Does this work similarly in PHP? – AndyPet74 Jul 14 '16 at 14:28
  • it depends on the ORM you use, but fetching tasks is pretty easy and straightforward. It might be like: $task = new Task(); $allTasks = $task->fetchAll(); And in this case, $allTasks is array of all tasks which are actually objects. But syntax depends on ORM. Doctrine is one very popular ORM and maybe you can see how it works in Symfony (framework) http://symfony.com/doc/current/book/doctrine.html – Marko Krstic Jul 14 '16 at 14:44