Page 1 of 1
Getting Classes down
Posted: Tue Jul 11, 2006 4:18 am
by Benjamin
Well, I've been writting classes the last week and have finally gotten a pretty solid grasp on it. I know someone will have me beat on this one, but this is the craziest 3 lines of code I have ever written. It does look pretty cool though heh. 2 months ago I would have saw this and been like
Code: Select all
if (!$this->validation->maxLength($this->username, $this->usernameMaxLength)) {
$this->pushError('Your username cannot be longer than ' . $this->usernameMaxLength . ' characters');
}
Re: Getting Classes down
Posted: Tue Jul 11, 2006 6:07 am
by Chris Corbyn
astions wrote:Well, I've been writting classes the last week and have finally gotten a pretty solid grasp on it. I know someone will have me beat on this one, but this is the craziest 3 lines of code I have ever written. It does look pretty cool though heh. 2 months ago I would have saw this and been like
Code: Select all
if (!$this->validation->maxLength($this->username, $this->usernameMaxLength)) {
$this->pushError('Your username cannot be longer than ' . $this->usernameMaxLength . ' characters');
}
IMO OOP certainly makes more readble code. Referring to things as objects with properties and methods sort of makes you think more about how the code actually works.
Re: Getting Classes down
Posted: Tue Jul 11, 2006 7:10 am
by Oren
d11wtq wrote:IMO OOP certainly makes more readble code. Referring to things as objects with properties and methods sort of makes you think more about how the code actually works.
Yes, exactly. When you see something like:
Code: Select all
if ($validator->length($data))
{
// more process
}
It's much easier to tell what the code does and how it works.
Posted: Fri Jul 14, 2006 5:08 pm
by feyd
I'd use an exception.

Posted: Sat Jul 15, 2006 8:05 am
by santosj
feyd wrote:I'd use an exception. ;)
Why? I know that if you used Java a lot that is what you would do, but I don't see the point of throwing Exceptions all around the place with no insight to possible error clauses. What he is checking for is bool, if the subclass didn't exist, then yeah, I would try an exception to let the developer know that a run time error occurred where there was no subclass to access.
Posted: Sat Jul 15, 2006 8:54 am
by feyd
santosj wrote:Why? I know that if you used Java a lot that is what you would do, but I don't see the point of throwing Exceptions all around the place with no insight to possible error clauses. What he is checking for is bool, if the subclass didn't exist, then yeah, I would try an exception to let the developer know that a run time error occurred where there was no subclass to access.
Well, for one, he's talking about OOP. Exceptions are better equipt for doing what he wishes in OOP. Exceptions will also allow you to break out of the functional block in more simple terms to a central location to handle the error as the lines of where they are handled are often quite clear.
The original post has little, if anything, to do with subclassing. There are other factors to consider as well: it can be an interesting challenge to support multiple languages in a single build. I personally use Exceptions to easily build localized versions of error outputs; not as multiple duplicates of the same exception with differing text embedded, but as centralized locations to pull up the localized strings. All categorized and neatly kept so all the errors are simple to alter and with simple subclassing to segment errors into differing categories internally -- FatalExceptions and RecoverableExceptions for instance.
Posted: Sat Jul 15, 2006 1:59 pm
by Benjamin
What would that look like? Are you saying use try/catch? Have an example?
Posted: Sat Jul 15, 2006 2:35 pm
by santosj
feyd wrote:Well, for one, he's talking about OOP. Exceptions are better equipt for doing what he wishes in OOP. Exceptions will also allow you to break out of the functional block in more simple terms to a central location to handle the error as the lines of where they are handled are often quite clear.
The original post has little, if anything, to do with subclassing. There are other factors to consider as well: it can be an interesting challenge to support multiple languages in a single build. I personally use Exceptions to easily build localized versions of error outputs; not as multiple duplicates of the same exception with differing text embedded, but as centralized locations to pull up the localized strings. All categorized and neatly kept so all the errors are simple to alter and with simple subclassing to segment errors into differing categories internally -- FatalExceptions and RecoverableExceptions for instance.
So for example:
Code: Select all
try {
$this->validation->maxLength($this->username, $this->usernameMaxLength);
} catch(InvalidUsernameLengthException $e) {
throw $e;
}
Hmm. That does look sexy, but I digress. What if you throw an
exception in a foreach loop? I do sometimes validate in a foreach block and have the class handle the fields. I have experimented with try ... catch when I see it as fit.
Having an Exception for each common Error reminds me too much of D or Java.
Okay, I admit I was wrong. I hope no one can read this.
astions wrote:What would that look like? Are you saying use try/catch? Have an example?
Man, I wrote my message before that, but it is pretty coincidental.
Posted: Sat Jul 15, 2006 2:45 pm
by Oren
astions wrote:What would that look like? Are you saying use try/catch? Have an example?
http://www.zend.com/php5/articles/php5-exceptions.php