From 5127a83935a9c9626c078d5323e9547fdb4290d6 Mon Sep 17 00:00:00 2001 From: Mikael Nordfeldth Date: Thu, 6 Mar 2014 14:21:44 +0100 Subject: [PATCH] Add Managed_DataObject getID() to avoid $obj->id --- classes/Managed_DataObject.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/classes/Managed_DataObject.php b/classes/Managed_DataObject.php index d17552212b..531232332d 100644 --- a/classes/Managed_DataObject.php +++ b/classes/Managed_DataObject.php @@ -298,4 +298,26 @@ abstract class Managed_DataObject extends Memcached_DataObject } return $ckeys; } + + /** + * Returns an ID, checked that it is set and reasonably valid + * + * If this dataobject uses a special id field (not 'id'), just + * implement your ID getting method in the child class. + * + * @return int ID of dataobject + * @throws Exception (when ID is not available or not set yet) + */ + public function getID() + { + // FIXME: Make these exceptions more specific (their own classes) + if (!isset($this->id)) { + throw new Exception('No ID set.'); + } elseif (empty($this->id)) { + throw new Exception('Empty ID for object! (not inserted yet?).'); + } + + // FIXME: How about forcing to return an int? Or will that overflow eventually? + return $this->id; + } }