Stan Catalin
Stan Catalin

Reputation: 51

OOP class private/protected

I want to display the number of the table, which I get from the HTML form, in this method: private function getComanda();.

I use public function displayMethod() to call the private function getComanda().

I want the table number to be displayed in the class Table, in the public function setMasa($nr_masa), which has a switch. It is not displaying anything at all.

When I try to display the table number with this function from Shop class, public function getProperty(), it works. I am stuck, can anyone help?

I tried to make the function getProperty() as both private and protected and it did not show any results, but if I change it to public it does.

This is the HTML code:

<html>
<body>
<form action="" method="post">
    <p>Preturi:</p><br/>
    Nr-Masa: <input type="text" name="nr_masa" /><br/>
    <input type="submit" value="Trimite" name="submit_masa" />
</form>
</body>
</html>

The class shop where:

$nr_masa=number of table; $_masa1=table1     

class Shop
{
    protected $nr_masa;
    private $_masa1;

    public function setComanda($nr_masa)
    {
        $this->_nr_masa = $nr_masa;
    }

    public function displayMethod()
    {
        $this->_masa1=$this->getComanda();
        print $this->_masa1;
    }

    private function getComanda()
    {
        return "<br /><br />Table number:" . $this -> _nr_masa . "<br />";
    }

    public function getProperty()
    {
        return $this -> _nr_masa . "<br />";
    }
}

class Table extends Shop
{
    public function setMasa($nr_masa)
    {
        switch($nr_masa) {
            case "1";
                echo "Masa Nr.1 a fost rezervata";
                echo $this -> displayMethod();
                break;
            case "2";
                echo "Masa Nr.2 a fost rezervata";
                echo $this -> displayMethod();;
                break;
            case "3";
                echo "Masa Nr.3 a fost rezervata";
                echo $this -> displayMethod();
                break;
            case "4";
                echo "Masa Nr.4 a fost rezervata";
                echo $this -> displayMethod();
                break;
            default:
                echo "Masa nu exista";
        }
    }
}



$TabelData = new Table;
$ShopData = new Shop;

if (isset($_POST['submit_masa'])) {
    $nr_masa = $_POST["nr_masa"];
    $TabelData -> setMasa($nr_masa);
    $ShopData -> setcomanda($nr_masa);
}

Upvotes: 0

Views: 64

Answers (1)

N Kumar
N Kumar

Reputation: 1312

you are using print in function displayMethod() and then using echo in function setMasa

public function displayMethod()
{
    $this->_masa1=$this->getComanda();
    return $this->_masa1;              <-- replace print with return;
}

Upvotes: 2

Related Questions