Jump to content

How can I return variable contents from one method to use in an extended class?


Recommended Posts

I'm not really sure of the terminology here. I am extremely new to OOP. I think the code will speak for itself here.

How can I get public $socket contents over to an extended class method?

<?PHP
class objSocket {
	public $socket;		
	function Server($host, $port) {
		//echo "created a socket: $host:$port<br>";
		$socket = fsockopen("udp://" . $host, $port);
		return $socket;
	}
}

class objTemp extends objSocket {
	function sendMsg($string) {
		//echo "sending: $string";
		fwrite($socket, $string);
	}
}

$tClient = new objTemp;
$tClient->Server("127.0.0.1", 23000);
$tClient->sendMsg("Hello World");
?>

try using $this->socket instead of $socket. When trying to use class scope data members, you need to quantify them with $this-> so PHP knows which scope the variable you are looking for is. In your example, $socket would try to refer to a variable defined or available in the local scope.

Oh I missread your code, and the stupid timer won't let me modify my first post anymore. You return the socket variable that you want to use. What is strange is that you have a socket data member that you never assign a value to. It seems you intend to assign ur socket data member a value, but didn't because you didnt know or realize my point in my first post. There are a couple ways you could fix your code, and both ways are really based on what style you want to code with. Either is valid of course.

 

I recommend this way

<?PHP
class objSocket {
	public $socket;	//whenever you refer to this variable, it must be quantified with $this->	
	function Server($host, $port) {
		//echo "created a socket: $host:$port<br>";
		$this->socket = fsockopen("udp://" . $host, $port);
		//return $socket; no need to return since our class now has access to this socket
	}
}

class objTemp extends objSocket {
	function sendMsg($string) {
		//echo "sending: $string";
		fwrite($this->socket, $string);
	}
}

$tClient = new objTemp;
$tClient->Server("127.0.0.1", 23000);
$tClient->sendMsg("Hello World");
?>

 

But you could also do

<?PHP
class objSocket {
	//public $socket; this becomes kind of useless since we dont assign it anything
	function Server($host, $port) {
		//echo "created a socket: $host:$port<br>";
		$socket = fsockopen("udp://" . $host, $port);
		return $socket;
	}
}

class objTemp extends objSocket {
	function sendMsg($string, $socket) {//since we no longer keep track of the socket within the class, we need to pass it in
		//echo "sending: $string";
		fwrite($socket, $string);
	}
}

$tClient = new objTemp;
$socket = $tClient->Server("127.0.0.1", 23000);//we need to catch the return value
$tClient->sendMsg("Hello World", $socket);//we need to pass our socket in since its no longer part of the class
?>

 

One fix versus the other is a debate that can get rather lengthy and really depends on your coding style

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.