Jump to content

PHP Empty Class to indicate object


dennis-fedco

Recommended Posts

I came across this type / style of code:

 

//EmptyObjectClass.php

class EmptyObjectClass

{
}

 

//other files:

require ("EmptyObjectClass.php");

 

class XClass
{

    function __construct()

    {

       $this->data = new EmptyObjectClass();

    }

 

    function manipulateData()

    {

       $this->data->value = 43;

    }

   ...

}

 

It seems that EmptyObjectClass is useless here and I can just remove any and all references to EmptyObjectClass to make the code cleaner.  It is pretty safe to do so now as I do not see any code that depends on that class.  i.e. there is no code like: if ($object instanceof EmptyObjectClass) {} and I am not aware of any other instanceof type operators that may be needed, but either way I can rewrite code where needed to not depend on this empty class.

 

One caveat I can see is that I may need to still declare things like $this->data, or $this->data->value somewhere, so that future coders who will deal with this class can see what the code operates on.

 

So here is my Question:   I can restructure the code however I want, so I have some freedom here, and I want to write good code.   Assuming that I can safely get rid of the EmptyObjectClass, and clean up the code, while I do so, what are the best practices that I can use?  Can you give me a code example?

 

 

Here is what I am thinking for now:  declare class properties in the class body and initialize them in costructor a la C++:

 

class XClass
{

    // property declarations:
    protected $data;

 

    function __construct(StatsObject $stats)

    {

       $this->data->value = $stats->value;

    }

 

    function manipulateData()

    {

       $this->data->value += 2;

    }

   ...

}

Edited by dennis-fedco
Link to comment
Share on other sites

For a generic object that can hold any property you can use stdClass rather than that EmptyObject class.

 

$object = new stdClass;
$object->blah = 'blah';
If possible however, you should create an object that declares the individual properties that may be set. Some issues with the generic approach like above are

- You may need to use isset() to prevent E_NOTICE errors when reading properties if you're not sure whether they were set yet

- You get no help from an IDE for auto-complete of property names

- You can't use type hinting to enforce a particular type of object

 

For example:

class SomeProperties {
   public $value;
}

class XClass {
   /** @var SomeProperties */
   protected $data;
   public function __construct(StatusObject $status){
      $this->data = new SomeProperties;
      $this->data->value = $status->value;
   }
}
If you want to make your code even stricter and prevent your structure objects from accepting anything other than the defined properties (possibly helpful to detect typos and such) you can do something like this:

abstract class StructureObject {
   final public function __get($nm){ throw new Exception('Invalid property '.$nm); }
   final public function __set($nm,$v){ throw new Exception('Invalid property '.$nm); }
}

class SomeObject extends StructureObject {
   public $value;
}
With that, if you happened to make a typo like $object->valeu = 'blah'; you'd get an exception rather than it silently creating that property and possibly causing problems later on.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.