Choosing an appropriate Zend Framework exception class -
using zend framework, want throw exception within particular method in model class if there arguments passed considered illegal method. in java, example, this:
public void addname(string name) { if (name.equals('')) { throw new illegalargumentexception(); } // other code if ok. }
however, far can see, php , zend framework lack such basic built-in exception classes illegalargumentexception
. should utilize pass exception describes went wrong? create such exception class myself? isn't kind of code framework supposed eliminate?
i'm starting learning zend framework. haven't wrote lot of php in life please sense free explain me things think should obvious decent php programmer.
here list of available exceptions in php spl exception class.
exception logicexception badfunctioncallexception badmethodcallexception domainexception invalidargumentexception lengthexception outofrangeexception runtimeexception outofboundsexception overflowexception rangeexception underflowexception unexpectedvalueexception
zend framework's zend_exception
wrapper php's built in exceptions, of major components have callable exception class.
for example:
public function setid($id) { $validator = new my_validator_id(); if ($validator->isvalid($id)) { $this->id = $id; homecoming $this; } else { throw new zend_validate_exception("$id not valid value id field."); } }
or php's built in excepition:
public function __get($name) { $property = strtolower($name); if (!property_exists($this, $property)) { throw new \invalidargumentexception( "getting property '$property' not valid entity"); } //truncated... }
zend framework 2 has more specific exceptions
available.
zend-framework
No comments:
Post a Comment