tags:

views:

66

answers:

4
+1  Q: 

PHP OOP problem

Hello

<?php
class Templater
{
    var $params = array();

    public static function assign($name, $value)
    {
        $this->params[] = array($name => $value);
    }

    public static dunction draw()
    {
        return $this->params;
    }
}


<?php
    $test = Templater::assign('key', 'value')->draw();
    print_r($test);

I need to function "assign" was static, but $params was common for the whole class.. BUt this code is not working((

Fatal error: Using $this when not in object context

Any ideas?

+6  A: 

It sounds like you want $params to be static:

<?php
class Templater
{
    static $params = array();

    public static function assign($name, $value)
    {
        self::$params[] = array($name => $value);
    }

    public static function draw()
    {
        return self::$params;
    }
}


<?php
    Templater::assign('key', 'value');
    $test = Templater::draw();
    print_r($test);
VoteyDisciple
Another error: Fatal error: Call to a member function draw() on a non-object in
Isis
You were calling ->draw(). I missed that. I've edited to correct.
VoteyDisciple
$test = Templaters::assign('key', 'value')->assign('key2', 'value2')->draw();How do I make it so I could use this code this way?
Isis
You have to completely eliminate static methods and properties in your code. See my sample below.
Rizo
+1  A: 

If you mean $params to be a static field, use:

class Templater {
  private static $params = array();

  public static function assign($name, $value) {
    self::params[] = array($name => $value);
  }

  public static dunction draw() {
    return self::params;
  }
}

static functions have no $this context.

By the way, don't use var for declaring instance variables. That's PHP4. Do it the PHP5 way.

cletus
+2  A: 

$this keyword refers to the class instance. When you are trying to call it inside a static method no class instance is used. So your assign method cannot be static to interact with $params, that is not static. Make $params static, or assign dynamic (not static).

<?php
class Templater
{
    static var $params = array();

    public static function assign($name, $value)
    {
        $this->params[] = array($name => $value);
    }

    public static dunction draw()
    {
        return self::params;
    }
}

or:

<?php
class Templater
{
    var $params = array();

    public function assign($name, $value)
    {
        $this->params[] = array($name => $value);
    }

    public dunction draw()
    {
        return $this->params;
    }
}

Both will work, but you must to choose the one that is more adequate for your application's design.

Rizo
+1  A: 

Singleton would work nice here

class Templater {
  private static $instance = null;
  private $params = array();

  public function __construct(){
    return $this;
  }

  public static function instance(){
    if(is_null(self::$instance)) self::$instance = new self();
    return self::$instance;
  }

  public function assign($name, $value){
    $this->params[$name] = $value;
    return $this;
  }

  public function draw(){
    return $this->params;
  }
}

Usage:

$test = Templater::instance()
  ->assign('var1', 'value1')
  ->assign('var2', 'value2')
  ->draw();
print_r($test);
Anpher