Getting Classes down

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Getting Classes down

Post 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 8O

Code: Select all

if (!$this->validation->maxLength($this->username, $this->usernameMaxLength)) {
    $this->pushError('Your username cannot be longer than ' . $this->usernameMaxLength . ' characters');
}
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Getting Classes down

Post 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 8O

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.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Re: Getting Classes down

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'd use an exception. ;)
santosj
Forum Contributor
Posts: 157
Joined: Sat Apr 29, 2006 7:06 pm

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

What would that look like? Are you saying use try/catch? Have an example?
santosj
Forum Contributor
Posts: 157
Joined: Sat Apr 29, 2006 7:06 pm

Post 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.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post 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
Post Reply