Call Functions Of Remote Class (OOP, CLI)

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
Syekiya
Forum Newbie
Posts: 4
Joined: Tue Jun 01, 2010 4:49 pm

Call Functions Of Remote Class (OOP, CLI)

Post by Syekiya »

I'm not really sure how to explain my situation, but I'll do my best...

As it stands, I'm new to OOP. What I have is the following...

Code: Select all

Class IRC
Class ModuleSystem
Class Core extends ModuleSystem

I have the following function inside of class IRC

Code: Select all

public function Write($Data) {
	fwrite($this->Connection, $Data);
}

and the following function inside of Core

Code: Select all

public function PassInfo($Command) {
	if ($Command == ":!test") {
		IRC::Write($Data = "PRIVMSG #SyeBot: Test!");
	}
}

As you can see, I want the function inside of Core to write to the socket via the function Write...

But when I do, it gets the following error:

Notice: Undefined property: Core::$Connection in /syebot/bot/class_IRC.php on line 33

Warning: fwrite(): supplied argument is not a valid stream resource in /syebot/bot/class_IRC.php on line 33


For some reason, it's trying to call the write command as Core:: when it should be calling it as IRC::


If you need me to further explain this, please let me know.


Thanks,
Syekiya
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Call Functions Of Remote Class (OOP, CLI)

Post by requinix »

Classes IRC and Core have nothing to do with each other in the code. There's no inheritance at all: a variable or method in one does not exist in the other.
Syekiya
Forum Newbie
Posts: 4
Joined: Tue Jun 01, 2010 4:49 pm

Re: Call Functions Of Remote Class (OOP, CLI)

Post by Syekiya »

Is there a way to link these classes without making Module an extended class?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Call Functions Of Remote Class (OOP, CLI)

Post by Christopher »

The error is simply that the IRC class does not have a Connection property defined, but it is being used in IRC::Write(). You should fix that and pass the connection to the constructor of the IRC class.

Also, a better design would be to pass an IRC object to the constructor of the Core class. Then you would not have the static call which is essentially procedural.
(#10850)
Syekiya
Forum Newbie
Posts: 4
Joined: Tue Jun 01, 2010 4:49 pm

Re: Call Functions Of Remote Class (OOP, CLI)

Post by Syekiya »

IRC:: does have a property.


It is set in:

Code: Select all

		public function Connect() {
			global $Bot_Info;
			echo("Attempting to connect to $this->SERVER on port: $this->PORT as SB_Branden\r\n");
			$this->Connection = fsockopen($this->SERVER, $this->PORT); // Connects to the IRC Server
			$this->Write("USER SB_Test 0 * :SyeBot\r\n"); // Sets the IDENT and REALNAME of the bot
			$this->Write("NICK SB_Test\r\n"); // Sets the NICK of the bot
			$this->Connected = true;
			echo("Connected! $this->SERVER\r\n");
		}

As you can see, I can use $this->Write within this class and it works fine, it is when I call it from the Core:: class that is messes up.



How it works is. This script connects to a server (IRC), the IRC:: class sends and recieves data to the server. I then have a second class (Modules) which is extendable by other classes (In this area, Core)...

I need Core to be able to use IRC::Write to send data to the server.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Call Functions Of Remote Class (OOP, CLI)

Post by Christopher »

Can you post the code, or at least simplified versions of these classes?
(#10850)
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Call Functions Of Remote Class (OOP, CLI)

Post by Weirdan »

The problem is that you're calling statically non-static method. When you do so from an instance of some object, $this in the called method is set to that instance (even if the method's class is different).
What you need is to have an instance of IRC class in your calling method and call Write on it (not statically).
Syekiya
Forum Newbie
Posts: 4
Joined: Tue Jun 01, 2010 4:49 pm

Re: Call Functions Of Remote Class (OOP, CLI)

Post by Syekiya »

Ok, I've noticed that I left out some details...


What I'm essential trying to do is have one object call a function of another object...


What my situation is:

I have one object (IRC), another object (Module System), and then an object for each Module loaded in the module system...

I need to have the ability of any of those module objects call a function in IRC (Writing to the server.)...
Post Reply