0

How can I retrieve GET values inside a PHP Class construct or functions?

Similar subject can be found here which I referred to session values, scope. Access Session Variable within a class

Example which doesn't retrieve value, set inside class construct.

$this->siteid = $_GET['siteid'];

or

$this->siteid = $_REQUEST['siteid'];

The purpose of the above is to retrieve id for testing params, goal is to hold these inside session, similar problem.

11
  • 4
    $_GET is a "superglobal"; it shouldn't make any difference whether it's inside a constructor, function or anything else. Does the variable exist inside $_GET when called elsewhere? If so, it will only not exist inside the constructor if you've unset it somewhere else. Commented May 21, 2011 at 13:21
  • Notice: Undefined index: siteid Commented May 21, 2011 at 13:25
  • @Codex73: this means it is undefined, obviously Commented May 21, 2011 at 13:41
  • That exact same code works before the class instantiation but doesn't work in the class' constructor? Commented May 21, 2011 at 13:42
  • @Codex73 Undefined index message its mean there is no name with it, make sure you have a variable called 'siteid' in your request. Commented May 21, 2011 at 13:44

1 Answer 1

0

Can you please check what does var_dump($_REQUEST). I think that variable isn't present in $_GET variable in that particular page in where you are accessing the class.

Sign up to request clarification or add additional context in comments.

4 Comments

array(1) { ["siteid"]=> string(2) "86" }
I just wrote <?php class A { var $siteid; public function __construct() { $this->siteid = 0; } public function setSiteId(){ $this->siteid = $_GET["siteid"]; } public function getSiteId(){ return $this->siteid; } } $a = new A(); $a->setSiteId(); var_dump($a->getSiteId()); ?> and this is working fine for me.
@Tapos Yes that works for your scenario. I have a slight different need which is assigning the request value to an inside object variable. It could be the order I'm calling processes.
It wasn't a problem with scope or global but with class instantiation order, overlapping with others. Also a ajax request passing a value which included variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.