Jump to content

OOP or Procedural?


Recommended Posts

I want to see your opinion about OOP and Procedural. Which method has more easier to code in PHP? I'm using Procedural, but I notice PHP can read OOP as C++. Which is better for PHP?

 

Thanks,

 

Gary

Edited by sigmahokies
Link to post
Share on other sites

Both paradigms are easy to understand, both are perfectly valid, and both are widely used in PHP.

 

The advantage of OOP is that it's more powerful than classical procedural code. When your applications grow and become complex, juggling with huge monolithic scripts and unprotected global variables doesn't work very well. You need a more abstract approach to keep the code organized. Namespaces and functions help a bit, but they don't really solve the problem. OOP is perfect for complex applications, because you can hide all the implementation details behind a simple interface and restrict variable access to those parts of the code which actually need it.

 

I think you should learn both paradigms and decide for yourself which one is appropriate for the concrete problem. Procedural programming is conceptually simpler and has less overhead, which is great for small applications. OOP makes sense for mid-sized and large applications.

 

Besides that, it depends a lot on your personal preferences (if you're working alone).

  • Like 1
Link to post
Share on other sites

Even on smaller projects once you have a library of classes built up it easier to either transfer the classes or have a centralized library of the classes. Thus saving you time and probably money if you're doing it for a client. 

  • Like 1
Link to post
Share on other sites

The only caveat I'd add to this discussion is don't discount a smaller and leaner approach without thinking it through. Some people, once they start to wrap their head around OOP, think that EVERY D**N THING needs to be an object, and that EVERY D**N OBJECT has to be GodLike.

That's ain't always so (and in the case of the latter, probably hardly *ever* is that so...).  And, you can have "lean" procedural and also have "lean" OO code....

I've seen "cool OOP designs" that are such resource hogs that a few lines of Procedural runs circles around them. It's all in the ability of the programmer to understand the problem and write the BEST code to meet the goal. That being said, some of my messes are procedural, and some of them are OO ... but I'm cleanin' em' up whenever I get the chance ... ;)

Edited by dalecosp
Link to post
Share on other sites

Here's a challenge for anyone using OOP so you can see how it benefits. I've started it off and you can see there are issues that need to be corrected.

 

An online shop needs an order dispatch system for sending out parcels using different couriers. At the start of the day a batch will be started and at the end of the day it will be closed when no more parcels are going to be shipped. This is called the dispatch period.

Each parcel sent out with a courier is called a consignment. Each consignment will be given a unique number and each courier will provide an algorithm for generating the number.

At the end of the dispatch period a list of all consignment numbers need to be sent to each courier. Each courier has their own method for accepting the list i.e. UPS uses email, Fedex uses FTP.

 

Build a structure using OOP to facilitate the implementation of the above scenario. It does not need to be a final product. Assume that your library is given to another developer to use who will build a front end for it that will have the following features:

1. Start a new batch

2. Add a consignment

3. End the batch

/**
 * Interface CourierInterface
 */
interface CourierInterface
{
    /**
     * Fetch a consignment number
     * @return int
     */
    public function fetchConsignmentNumber();

    /**
     * Dispatch a consignment number to a courier
     */
    public function dispatch($number);
}
class FedexCourier implements CourierInterface
{
    /**
     * @var int
     */
    private static $count = 10000;

    /**
     * We will use a simple static counter here to return a number
     * @return int
     */
    public function fetchConsignmentNumber()
    {
        self::$count++;
        return self::$count;
    }

    /**
     * Send to courier
     * @param $number
     * @return string
     */
    public function dispatch($number)
    {
        
    }
}
class Consignment
{
    /**
     * @var CourierInterface
     */
    protected $courier;

    /**
     * @var int $number  Should contain a consignment number
     */
    protected $number;

    /**
     * Consignment constructor.
     * @param CourierInterface $courier
     */
    public function __construct(CourierInterface $courier)
    {
        $this->courier = $courier;
        // fetch a consignment number from the courier
        $this->number = $this->courier->fetchConsignmentNumber();
    }

    /**
     * Simple getter
     * @return int
     */
    public function getNumber()
    {
        return $this->number;
    }

    /**
     * @return mixed
     */
    public function process()
    {
        return $this->courier->dispatch($this->number);
    }
}
class Batch
{
    /**
     * @var array
     */
    protected $consignments = [];

    /**
     * Batch constructor.
     * Start the dispatch period
     * This could take in a storage dependency for persistence
     */
    public function __construct()
    {

    }

    /**
     * Add a consignment to the current batch
     *
     * @param Consignment $consignment
     */
    public function addConsignment(Consignment $consignment)
    {
        $this->consignments[] = $consignment;
    }

    /**
     * Process each consignment
     */
    public function endDispatchPeriod()
    {
        // loop through each of the consignments
        // the storage dependency could be use here to track the consignment numbers
        // i.e. $c->getNumber()
        foreach ($this->consignments as $c) {
            $c->process();
        }
    }

}

Client

<?php
// client code

// start a new batch
$batch = new Batch();

// add some consignments
$parcel1 = new Consignment(new FedexCourier());
$batch->addConsignment($parcel1);

$parcel2 = new Consignment(new FedexCourier());
$batch->addConsignment($parcel2);

// end the batch
$batch->endDispatchPeriod();
?>
Link to post
Share on other sites
This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.