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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

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

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

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

Post 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.
(#10850)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

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

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply