From: Mikael Nordfeldth Date: Thu, 6 Mar 2014 13:21:44 +0000 (+0100) Subject: Add Managed_DataObject getID() to avoid $obj->id X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=5127a83935a9c9626c078d5323e9547fdb4290d6;p=quix0rs-gnu-social.git Add Managed_DataObject getID() to avoid $obj->id --- 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; + } }