denii
denii

Reputation: 99

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 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:

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 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:

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 classes 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

Upvotes: 1

Views: 107

Answers (1)

DevMan
DevMan

Reputation: 556

answering all your questions needs a book as understanding the oop concept completely needs much reading and practicing. But to answer your post class I give you an easy to use example where you can create a class which will help you create, read, delete and update posts. This class can be used in all your projects as it's a class and classes are simply there to make our life easier.

So here we go: first we create the class:

class posts
{
    //we just need a post title and a post content for the new post.
    //So we create two properties for post_title and post_content
    private $post_title; //the variables are private so we we restrict access the them from outside the class.
    private $post_content;
    private $connection;
    //you really don't need here the _constructor function, but it's your choice.
    //For example you can do all your database connection stuff in the constructor method to make it easy for users.
    //So if someone creates a posts object it is obvious that he's going to make some interactions with the database so you open connections in your constructor.
    //next we create the methods for our posts class

    public function __constructor()
    {
        //here you created the connection to your database. You can retrieve
        //the parameters to the mysqli_connect from a eg. config.php file to make your class more dynamic.
        $this->connection = mysqli_connect('hostname', 'username', 'password', 'databasename');
    }

    public function create_post($title, $content)
    {
        $this->post_title=$title;
        $this->post_content=$content;
        $query="INSERT INTO posttable(id,title,content) VALUES('$this->post_title','$this->post_content')";
        $result=$mysqli_query($this->connection,$query);
    }

    public function update_post($post_id, $post_title, $post_content)
    {

    }

    public function delete_post($post_id)
    {

    }

    public function view_posts($query, $fields)
    {
        $result=mysqli_query($this->connection, $query);
        $finalData=array();
        while ($row=mysqli_fetch_assoc($data)) {
            foreach ($fields as $field) {
                array_push($finalData, $row[$field]);
            }
        }
        return $finalData;
    }
}

//now we're going to use the class.
$post=new posts(); //a new posts object is created and the __construct method opened connection to db.
$post->create_post('some title', 'some content');//inserted new post into database.
$post->update_post($id, $content, $title);//you've just updated a post
$post->delete_post($id);//you've just deleted a post.

//getting posts 
$query="SELECT * FROM posts";
$fields=array('id','title','content');

$all_posts=$post->view_posts($query, $fields);
//now you have $all_posts which is filled with an array of all posts from the posts table.
//now you can loop through it and show it on your page.
for ($i=0; $i<=count($all_posts); $i++) {
    echo '<h1>'.$mydata[$i].'</h1>';
    $i++;
    echo '<h2>'.$mydata[$i].'</h2>';
    $i++;
    echo '<h3>'.$mydata[$i].'</h3>';
}

I didn't implement the update and delete method code but I think it's easy for you to write it by yourself.All my goal was to show you how you can use oop in your web applications. I personally have nearly 50 classes written by myself and do all the major jobs for nearly all possible web application tasks. It's up to you how you create your classes. Just for your knowledge, oop programming needs training and experience. You can do a task in multiple forms and with different solutions. You can find dozens of books which teach you oop design who's concept and goal is to teach you how to design a class to the best way. I hope I was helpful

Upvotes: 1

Related Questions