Jump to content

Why doesn't GET work with my class?


Perad

Recommended Posts

If i have this it works and it echo's out the 2 wrong statements.

 

class FilterPage {
private $all;
private $id;

public function getpage() {
	if($this->all == "1") {
		echo "all right";
	} else {
		echo "all wrong";
	}
	if($this->id == "1") {
		echo "id right";
	} else {
		echo "id wrong";
	}
	echo "Nothing";
}
}

 

However i want to use $_GET as below but it just returns nothing. Does anyone know why this is?

 

class FilterPage {
private $all = $_GET['all'];
private $id = $_GET['id'];

public function getpage() {
	if($this->all == "1") {
		echo "all right";
	} else {
		echo "all wrong";
	}
	if($this->id == "1") {
		echo "id right";
	} else {
		echo "id wrong";
	}
	echo "Nothing";
}
}

Link to comment
https://forums.phpfreaks.com/topic/48918-why-doesnt-get-work-with-my-class/
Share on other sites


<?php

class FilterPage {

private $all;
private $id;

public function getpage() {

  $this->all = $_GET['all'];
  $this->id = $_GET['id'];

  if($this->all == "1") {
   echo "all right";
  } else {
   echo "all wrong";
  }

  if($this->id == "1") {
   echo "id right";
  } else {
   echo "id wrong";
  }

  echo "Nothing";

}

}

$_temp = new FilterPage();

echo $_temp->getpage();

?>

Another way would to be to use a constructor:

<?php

class FilterPage {

    private $all;
    private $id;

    // this will get called automatically when the class is initiated.
    public function FilterPage()
    {
        $this->all = $_GET['all'];
        $this->id = $_GET['id'];
    }

    public function getpage()
    {
        if($this->all == 1)
        {
            echo "all right";
        }
        else
        {
            echo "all wrong";
        }

        if($this->id == 1)
        {
            echo "id right";
        }
        else
        {
            echo "id wrong";
        }

        echo "Nothing";
    }
}

$_temp = new FilterPage();

echo $_temp->getpage();

?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.