FalseProphet Posted June 26, 2011 Share Posted June 26, 2011 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"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/240482-how-can-i-return-variable-contents-from-one-method-to-use-in-an-extended-class/ Share on other sites More sharing options...
mikesta707 Posted June 27, 2011 Share Posted June 27, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/240482-how-can-i-return-variable-contents-from-one-method-to-use-in-an-extended-class/#findComment-1235182 Share on other sites More sharing options...
mikesta707 Posted June 27, 2011 Share Posted June 27, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/240482-how-can-i-return-variable-contents-from-one-method-to-use-in-an-extended-class/#findComment-1235191 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.