title: Object oriented use of properties/methods in real webapplications?
body: <p>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.</p>
  
  <p>I was told the concept of OOP is basically close to the real world.
  So properties are just properties of any object and methods are like what the object should do. But I can't really understand how to use these properties and methods in a web app. In the real world, it's easier than in a web application for me, for instance:</p>
  
  <p>Let's say we want to create a human named "Paul" and he should drive a car.</p>
  
  <pre><code>class human
  {
  
      public $gender;
      public $name;
  
      function __construct($inGender=null, $inName=null) {
  
          $this-&gt;gender = $inGender;
          $this-&gt;name = $inName;
  
      }
  
      public function driveCar() {
  
      //Let the car be driven.
      //...
  
      }
  
  }
  
  $paul = new human('male','Paul');
  $paul-&gt;driveCar();
  </code></pre>
  
  <p>Alright, but when it comes to an object in a web application, let's say I want to code a blog system specifically 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:</p>
  
  <pre><code>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-&gt;id = $inId;
              }
              if(!empty($inTitle)) {
                  $this-&gt;title = $inTitle;
              }
              if(!empty($inContent)) {
                  $this-&gt;content = $inContent;
              }
              if(!empty($inTags)) {
                  $this-&gt;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
  
  &lt;?php foreach($posts as $post): ?&gt;
      &lt;h1&gt;&lt;?= $post-&gt;title;?&gt;&lt;/h1&gt;
      &lt;p&gt;&lt;?= $post-&gt;content;?&gt;&lt;/p&gt;
  &lt;?php endforeach; ?&gt;
  </code></pre>
  
  <p>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?</p>
  
  <p>Alright, but another question. How would I program the create/delete posts functionality, new classes or new methods? I can't really imagine anything of this in the real world concept.</p>
  
  <p>This all is not that clear for me since it's nowhere explained on any tutorial.</p>
  
  <p>Help would be amazing.</p>
  
  <p>regards</p>