samedi 5 mars 2016

Object oriented use of properties/methods in real webapplications?

I just moved from my "everything-in-functions" coding style to OOP in PHP. I know, there are a lot of sources in order to code properly, but my main problem is, after reading so many tutorials about coding a blog/cms/any web app in OOP it's quite not that understandable to me to code it the right way.

I was told the concept of OOP is basically close to the real world. So properties are justp roperties of any object and methods are like what the object should do. But I cant't really understand how to use these properties and methods in a webapp. In the real world, it's easier than in a webapplication for me, for instance:

Let's say we want to create a human named "Paul" and he should drive a car.

class human
{

    public $gender;
    public $name;

    function __construct($inGender=null, $inName=null) {

        $this->gender = $inGender;
        $this->name = $inName;

    }

    public function driveCar() {

    //Let the car be driven.
    //...

    }

}

$paul = new human('male','Paul');
$paul->driveCar();

Alright, but when it comes to an object in a webapplication, let's say I want to code a blog system specificially let's focus the posts. Properties could be id,name,content,author of a post, BUT what does a post actually do? It can't really perform an action so I would code it like this:

class post
{

    public $id;
    public $title;
    public $content;
    public $tags;

        //Overloading the Variables
        function __construct($inId=null, $inTitle=null, $inContent=null, $inTags=null) {

            if(!empty($inId) || $inId == 0) {
                $this->id = $inId;
            }
            if(!empty($inTitle)) {
                $this->title = $inTitle;
            }
            if(!empty($inContent)) {
                $this->content = $inContent;
            }
            if(!empty($inTags)) {
                $this->tags = explode(' ', $inTags);
            }           

        }

}

//In order to pass multiple posts to an constructor I would use a foreach loop

$posts[] = new post(0,'Hello World', 'This is a test content','test blog oop');
$posts[] = new post(1,'Hella World!', 'This is made my myself','test test test');

//Usage in a template

<?php foreach($posts as $post): ?>
    <h1><?= $post->title;?></h1>
    <p><?= $post->content;?></p>
<?php endforeach; ?>

The problem here is the method does not do anything, what is not supposed to do I guess. And another problem would be, when I would store data into an array, I can't use a single array with data of posts as several parameters for the constructor. Even if this was a single post, how to accomplish the passing of data as an array properly in order to use the array as the parameters? Maybe I just coded it not the best way, but what would be a better way? I heard of setter and getter and magic methods. What would here best practise?

Alright, but another question. How would I program the create/delete posts functionality, new clases or new methods? I can't really imagine anything of this in the real world concept.

This all is not that clear for me since it's nowhere explained on any tutorial.

Help would be amazing.

regards

Aucun commentaire:

Enregistrer un commentaire