first off I'm totally new to oop, so apologies for the bad code.

I've written a PHP script that returns a JSON object (based on mock data atm but will use a db later on) I have an ajax function on the front end that recieves the JSON data and turns it into a table.. The script it working totally fine.. But, as I say, I'm new to OOP and trying to get as much info as I can in writing good OOP.. Do you have any pointers as to how I can improve my code? I know I can make some improvements on error checking, but I'm looking for any guidance you can give on good OOP design.

<?php

class Person
{


    public function __construct()
    {

    }

    function __set($property, $value)
    {

        $this->$property = $value;

    }


    function __get($property)
    {

        if (isset($this->$property)) {

            return $this->$property;

        }

    }

    public function getArrayData()
    {

        $arr = array();

        foreach (get_object_vars($this) as $name => $value) {

            if ($name == 'department' && $value == 'admin') {
                $value = '';
            }
            $arr[$name] = $value;

        }
        return $arr;
    }


}



class handleData
{

    public $_data;


    public function __construct($data)
    {

        $this->_data    = $data;
        $this->_dataArr = array();

        for ($x = 0; $x < count($this->_data); $x++) {

            $this->_dataArr[$x] = serialize($this->_data[$x]);

        }

    }

    public function getJSON()
    {

        foreach ($this->_dataArr as $value) {

            $object = unserialize($value);
            $obj[]  = $object->getArrayData();

        }

        $data = array(
            'people' => $obj
        );
        return json_encode($data);

    }



}



$name1 = new Person();

$name1->name       = 'mark';
$name1->email      = '[email protected]';
$name1->photo      = 'mark.jpg';
$name1->department = 'admin';

$name2 = new Person();

$name2->name       = 'steve';
$name2->email      = '[email protected]';
$name2->photo      = 'steve.jpg';
$name2->department = 'marketing';


$mydata = new handleData(Array(
    $name1,
    $name2,
    $name3,
    $name4
));

echo $mydata->getJSON();
share|improve this question
1  
Your question might get a better reception over at codereview.stackexchange.com – Bart van Ingen Schenau 2 days ago

closed as off topic by MainMa, Martijn Pieters, Bart van Ingen Schenau, Kilian Foth, Jalayn 2 days ago

Questions on Programmers Stack Exchange are expected to relate to software development within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

1 Answer

While purely procedural designs without much modularity run just fine, the advantages of OO design show up in the maintenance. Because a typical application will spend the bulk of its lifetime in maintenance, code maintenance is a large expense over the lifetime of an application.

Modularity — one of the key characteristics of good OO design — helps with this maintenance. Modularity helps encapsulate change, which will make it easier to extend and modify the application over time.

In this great article from IBM you can read about seven habits that will help you get started in the transition between procedural programming and OO programming.

Build seven good object-oriented habits in PHP

share|improve this answer
I feel compelled to add one thing to that article: "composition over inheritance". Unless there is a good reason, do not inherit from a class implementing an interface. Instead, just implement the interface, and put the other class as private member, and add delegate methods. When you do use inheritance, prefer having an an abstract base class to inherit from, do not inherit from a concrete implementation class. – hyde 2 days ago
Looking at the samples in the first part, one cannot notice how broken PHP is. A language which forces to write so much boilerplate, repetitive code just to conform to best practices is doomed to create a community of bad programmers writing bad code. – MainMa 2 days ago

Not the answer you're looking for? Browse other questions tagged or ask your own question.