dennis-fedco Posted February 17, 2014 Share Posted February 17, 2014 (edited) 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 February 17, 2014 by dennis-fedco Quote Link to comment https://forums.phpfreaks.com/topic/286266-php-empty-class-to-indicate-object/ Share on other sites More sharing options...
gristoi Posted February 18, 2014 Share Posted February 18, 2014 dependancy injection is definately the way to go. if you need to use a blank object you can instantiate an instance of stdClass() Quote Link to comment https://forums.phpfreaks.com/topic/286266-php-empty-class-to-indicate-object/#findComment-1469381 Share on other sites More sharing options...
kicken Posted February 18, 2014 Share Posted February 18, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/286266-php-empty-class-to-indicate-object/#findComment-1469404 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.