Thew
Thew

Reputation: 15959

Set variables in a class

I wanna do something like this:

<?php
    $editor = new editor('reply.php?topic=100', 'simple');
    echo $editor;
?>

But im not fammilliar with OOP / Classes, but i wanna do something like this in the class:

<?php
class editor($url, $type)
{
    if($type == 'simple'){
        ?>
            <form action="<?php echo $url; ?>">
                ...
            </form>
        <?php
    }
    else
    {
        ...
    }
}
?>

Ofcourse, this isn't right. But how can i make it? Because i don't understand anything about constructors and destructors etc...

Upvotes: 2

Views: 125

Answers (3)

svoop
svoop

Reputation: 3454

This is very basic and essential stuff, so it really makes sense if you familiarize yourself with OO syntax of PHP. I'm sure, almost everybody could supply you with a canned answer, but still do yourself a favor and try to answer this simple question yourself. Here are the official docs:

http://es2.php.net/manual/en/language.oop5.basic.php

Upvotes: 2

zerkms
zerkms

Reputation: 254886

$editor = new editor('reply.php?topic=100', 'simple');
echo $editor;

class editor
{
    private $url;
    private $type;

    public function __construct($url, $type)
    {
        $this->url = $url;
        $this->type = $type;
    }

    public function __toString()
    {
        if($this->type == 'simple'){
            return '<form action="' . $this->url . '"></form>';
        } else {
            return 'foobar';
        }
    }
}

Upvotes: 4

Alex Pliutau
Alex Pliutau

Reputation: 21957

class MyClass
{
    private $_var;
    public function __constructor($var)
    {
        $this->_var = $var;
    }
    public function action()
    {
        echo $this->_var;
    }
}
$obj = new MyClass('abcd');
$obj->action();

Upvotes: 0

Related Questions