Jump to content

how to use a function within a function?


Go to solution Solved by Ch0cu3r,

Recommended Posts

Hi I got two functions and I want to use the 2nd function within the first function is this possible

 

Function 1

class poll {

    public $db;
    public $tbl;
    public $pollvars;
    public $poll_view_html;
    public $poll_result_html;
    public $options;
    public $options_text;
    public $poll_question;
    public $form_forward;
    public $template_set;
    public $ip;

    public function __construct() {
        global $POLLTBL, $CLASS;
        $this->tbl = $POLLTBL;
        $this->poll_view_html = array();
        $this->poll_result_html = array();
        $this->options = array();
        $this->options_text = array();
        $this->poll_question = array();
        $this->form_forward = basename($_SERVER['PHP_SELF']);

        $this->ip = getenv("REMOTE_ADDR");
        $this->db = &$CLASS["db"];
        $this->pollvars = $this->db->fetch_array($this->db->query("SELECT * FROM ".$this->tbl['poll_config']));
        $this->template_set = "default";
        
        if ($this->pollvars['result_order'] == "asc") {
            $this->pollvars['result_order'] = "ORDER BY votes ASC";
        } elseif ($this->pollvars['result_order'] == "desc") {
            $this->pollvars['result_order'] = "ORDER BY votes DESC";
        } else {
            $this->pollvars['result_order'] = '';
        }
    }

Function 2

function getUserIP()
{
$mxitidun = $_SERVER['HTTP_X_MXIT_DEVICE_ID'];
    $client  = @$_SERVER['HTTP_CLIENT_IP'];
    $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
    $remote  = $_SERVER['REMOTE_ADDR'];

    if($mxitidun != '')
    {
        $ip1 = $mxitidun;
    }

    elseif(filter_var($client, FILTER_VALIDATE_IP))
    {
        $ip1 = $client;
    }
    elseif(filter_var($forward, FILTER_VALIDATE_IP))
    {
        $ip1 = $forward;
    }
    else
    {
        $ip1 = $remote;
    }

    return $ip1;
}

I'm trying to Use the line in function 1

$this->ip = getenv("REMOTE_ADDR");

and replace getenv("REMOTE_ADDR"); with the function output value of Function 2  

Link to comment
https://forums.phpfreaks.com/topic/298291-how-to-use-a-function-within-a-function/
Share on other sites

  • Solution

You would call your function like any other function

$this->ip = getUserIP();

Or if  getUserIP is a method for your poll class then it'll be

$this->ip = $this->getUserIP();
Edited by Ch0cu3r

 

You would call your function like any other function

$this->ip = getUserIP();

Or if  getUserIP is a method for your poll class then it'll be

$this->ip = $this->getUserIP();

Ah thank you I see I had the same function on both pages and got redeclare error so I fixed it

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.