General Question

frankielaguna's avatar

Referencing a class from another class [PHP]?

Asked by frankielaguna (256points) November 14th, 2008
10 responses
“Great Question” (1points)

Hi! I’m trying to find out the best way to reference a class from within another class, IE: I have a base class called base, and I have a users class called users,

Within users theres: _uidToUname() which uses $this->_userExists();

I want to be able to use _uidToUname from Base.

Now If I do:
Users::_uidToUname(1); I get a, “Can’t use $this” error.

I don’t necessarily want to extend base to users.

Is there another way, or which is the best method to use?

Thanks!

Observing members: 0
Composing members: 0

Answers

amurican's avatar

Great question! I wish I knew.

funkdaddy's avatar

I may be misinterpreting what you’re asking, but can’t you declare an instance of users and then call the method off of that instance?

So…

$instance = new users();
$user_id = $instance->_uidToUname(1);

I believe that would give the $this statement a reference and should get you through there.

Would that supply what you’re looking for?

jasonjackson's avatar

The error is because you’re calling _uidToName() as a “static” method of the Users class, and it then calls a method which isn’t static (i.e. which uses $this; but since you called it statically, there is no $this to reference). So funkdaddy is right, declaring an instance of Users and calling through that should do what you need.

Unless Base is the base/super class for the Users class (i.e. Users extends Base), and you’re trying to use polymorphism to call _uidToName in the subclass (Users) from an instance of the base class (Base)?

Vincentt's avatar

It’d be helpful to know what the “Base” class is supposed to do.

However, if you find you need to call this method from another class without needing an instance of the first class, you might want to do some refactoring – i.e. moving the method elsewhere.

I’m not sure where you get the uid from in the Base class and what the Users class does. If you could provide that information we could probably give some hints as to how you’ll want to refactor it.

frankielaguna's avatar

My base class is being used in my app for clocking users in and out, It’s basically all the methods used for that. So it’s the base of this app.

So that being said I need to call _uidToUname when I’m writing to my logs who clocked in and at what time. Although it will be used for more things than that.

I think the best way is tohave Base Extend Users since it needs to use methods from the users class.

Still learning classes, Thanks for all your help. You cleared up a lot for me..

Vincentt's avatar

> “It’s basically all the methods used for that”

Does that mean it’s full of static methods? Because that’s how I started when I was learning OOP. I was using classes, but not really doing OOP. The main idea behind class is to have them revolve around data, i.e. “represent something” which you can alter using its methods.

Having said that, I’m still not sure what the “Users” class does, but if it represents a user then I’d say add the logging methods to the Users class, because that really concerns users.

funkdaddy's avatar

While we’re at it, I’ve noticed a lot of method names starting with an underscore (and double underscore) in other people’s code. Is there a purpose to that? Is it just a naming convention, and if so what would it mean? Does it have some built in functionality in PHP?

Just something I’ve been wondering.

frankielaguna's avatar

@funkdaddy:
It’s a naming convention. In PHP 4 there weren’t private methods.

So people would distinguish private methods from others by adding an underscore in front of the method name.

At least so I’ve been told. It kind of carried over to PHP 5 for me.

@vincentt:
True, I’m still getting into OOP so I don’t think I’m using it correctly. But for now I’m using it to organize my code a little bit. All the tutorials I’ve read on OOP in PHP haven’t helped much at all.

The users class holds all the user stuff like user id’s to usernames, logging in, and out, etc..

Vincentt's avatar

@funkdaddy – I use it as well for private and protected methods.

@frankielaguna – I know! All those tutorials explained the syntax of classes, but not how and why to use OOP, and what the best way is. The way I’ve learned it is by using the Zend Framework for a small project to get used to the “Model-View-Controller” architecture which made it immediately clear how useful classes are.

How does the Users class get its data? How does it provide it? Is it perhaps possible to send me the code of the two classes so I can take a look at it?

frankielaguna's avatar

@Vincentt It get’s its data from my MySQL DB. I’m more than happy to share my code with you. I’ve tried MVC frameworks before and I just don’t like them. But I have not had the time to really sit down and learn/use one. So I guess I should do that.

Answer this question

Login

or

Join

to answer.

Mobile | Desktop


Send Feedback   

`