Page 1 of 1

Simple OOP question - using include()d files in a class

Posted: Mon May 14, 2012 3:25 pm
by mecha_godzilla
Hi,

I hope this isn't too dumb a question, but here goes... :D

In my application I have a library class that contains all the common functions used by every section of my site, and when I try to access (say) the login page, a "login" class is called that extends the library class - the top of each script looks like this:

Code: Select all

class Login extends BasicFunctions {
// do login specific stuff here
}

class MyAccount extends BasicFunctions {
// do my account specific stuff here
}
The problem I have now is that the BasicFunctions class is becoming very big and unwieldy, so what I'd like to do is to split some of its functions into different files - one for authentication, one for form validation, etc.

What I can't work out is how to include() these within the class without losing the local reference, so if "authentication.php" contains a function called CheckUserAgent() that was originally defined in the BasicFunctions class I need it to be accessible from $this->CheckUserAgent().

Can anyone offer any suggestions please?

Thanks in advance,

Mecha Godzilla

Re: Simple OOP question - using include()d files in a class

Posted: Mon May 14, 2012 9:22 pm
by requinix
What you're doing is not object-oriented programming. "Login" is a verb, not a noun. "My account" is a section on your website, not an entity.

Just use normal functions or, if you're really keen on using classes (which is not how the rest of PHP works), stick the functions in an abstract utility-type class and make them static.

Re: Simple OOP question - using include()d files in a class

Posted: Mon May 14, 2012 10:14 pm
by Christopher
First, +1 to what requinix said.
mecha_godzilla wrote:The problem I have now is that the BasicFunctions class is becoming very big and unwieldy, so what I'd like to do is to split some of its functions into different files - one for authentication, one for form validation, etc.
This is a predictable result. Remember, OOP means data + the code a operates on that data. Think data first. What is the data associated with BasicFunctions? Or Login? Neither of those make much sense. However, something like Account or User sounds more like an object. I would divide all that code into classes based on the data each operates on. Then instantiate those utility classes your main classes where you need them.

Re: Simple OOP question - using include()d files in a class

Posted: Tue May 15, 2012 11:15 am
by pickle
PHP won't allow you to split a class definition between files, or even separate PHP blocks.

Also +1 to what ~Christopher said.