Page 1 of 1

Call Functions Of Remote Class (OOP, CLI)

Posted: Tue Jun 01, 2010 4:52 pm
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

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

Posted: Tue Jun 01, 2010 6:23 pm
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.

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

Posted: Tue Jun 01, 2010 6:31 pm
by Syekiya
Is there a way to link these classes without making Module an extended class?

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

Posted: Tue Jun 01, 2010 7:06 pm
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.

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

Posted: Tue Jun 01, 2010 7:41 pm
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.

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

Posted: Wed Jun 02, 2010 1:59 am
by Christopher
Can you post the code, or at least simplified versions of these classes?

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

Posted: Wed Jun 02, 2010 4:39 am
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).

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

Posted: Wed Jun 02, 2010 12:01 pm
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.)...