wek
wek

Reputation: 57

how can we use $_POST variable in a class

I was just learning class in PHP and so I am messing around with it. I just tried to get a value from a user and display it using a class. However, when I tried to use $_POST variable inside the class it shows an error.

Here is the code:

<form action="classess.php" method="POST" >
<b>Enter the rate : </b>
<input type="text" name="price" />
<input name="submit" type="submit" value="Click" />
</form>
<?php

class rating
{
  public $rate = $_POST['price'];
  public function Display()
  {
    echo $this -> rate;
  }
}

$alex = new rating;
$alex ->Display();
?>

Upvotes: 4

Views: 13945

Answers (5)

statement public $rate = $_POST['price']; can not be initialized
why? because variable initializations will happen in compile time when code is not interpreted yet and so the compiler knows nothing from $_POST['price'] at the time that the initialization is going on


this is also pointed out in php documentation:

This declaration may include an initialization, but this initialization must be a constant value.

Upvotes: 0

adhityan
adhityan

Reputation: 1

<?php
    class rating
    {
      public $rate;
    }

    $alex = new rating;
    $alex->rate=$_POST['price'];
?>

Result can be obtained simply with this method.

Upvotes: 0

Ahmed Habib
Ahmed Habib

Reputation: 189

This is your correct HTML part

<form action="classess.php" method="POST" >
<b>Enter the rate : </b>
<input type="text" name="price" />
<input name="submit" type="submit" value="Click" />
</form>

This is your corrected PHP part

<?php
class Rating
{
  public $rate;
  public function __construct() {
    $this->$rate = $_POST['price'];
  }
  public function display()
  {
    echo $this -> rate;
  }
}

$alex = new rating;
$alex ->Display();
?>

Let me explain it..

public function __construct() {
    $this->rate = $_POST['price'];
  }

is setting up your variables i.e constructing the class..

public function display()
  {
    return $this->rate;
  }

This function inside classes actually getting the value of var $rate

$alex = new rating;
echo $alex->display();

then simply init the class and use the function.

Upvotes: 2

Madara&#39;s Ghost
Madara&#39;s Ghost

Reputation: 174967

You cannot have statements inside of property definitions. Use a constructor instead:

class Rating {
    public $rate;

    public function __construct($price) {
        $this->$rate = $price;
    }

    public function display() {
        echo $this->rate;
    }
}

$alex = new Rating($_POST['price']);
$alex->display();

Some points:

  • Don't make it hard on yourself. If you need something to make the class work, ask it in the constructor.
  • ClassNames are usually written in CapitalCase, and methodNames are written in camelCase.
  • It might be preferable for the display() function to actually return the rate, instead of echoing it. You can do more stuff with a returned value.

Upvotes: 20

koerbcm
koerbcm

Reputation: 340

You're trying to assign a value in the wrong place. You need to assign your value in the constructor.

Why not do it this way?

<form action="classess.php" method="POST" >
<b>Enter the rate : </b>
<input type="text" name="price" />
<input name="submit" type="submit" value="Click" />
</form>
<?php

class rating
{
  var $rate;

  function rating($price)
  {
    $this->rate = $price;
  }

  public function Display()
  {
    echo $this->rate;
  }
}

$alex = new rating($_POST['price']);
$alex->Display();
?>

This way you can initialize the value when you create the object. It gives you a little bit more flexibility.

Upvotes: 1

Related Questions