Jump to content

How to reference a variable within a function within a class?


jdlev
Go to solution Solved by AbraCadaver,

Recommended Posts

Let's say I wanted to pull the host & address variables out of the function "ip" and echo them outside of the class. How would I go about doing that?  I tried in the code below and failed miserably :(
<?php
//Returns information on the User's IP Address, Hostname, Port, and Username

class getIP
 
{
	public function ip() { 
						$this->address = $_SERVER['REMOTE_ADDR'];
						$this->host = $_SERVER['REMOTE_HOST'];
						}
	
}

$addy = new getEm;
echo $addy->ip(this->address) . "</br>";

?>

 

Link to comment
Share on other sites

  • Solution

Well your function is setting the class vars so you need to call the function and then echo the vars.

$addy = new getIP;
$addy->ip();
echo $addy->address;

Not sure of your intent but probably more like this to set on instantiation:

class getIP {
    private $address = '';
    private $host = '';

    function __construct() {
        $this->address = $_SERVER['REMOTE_ADDR'];
        $this->host = $_SERVER['REMOTE_HOST'];
    }

    function __get($var) {
        return $this->{$var};
    }
}
$addy = new getIP;
echo $addy->address;
Edited by AbraCadaver
Link to comment
Share on other sites

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.