1

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

    public static function 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?

4 Answers 4

7

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);
Sign up to request clarification or add additional context in comments.

4 Comments

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

$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.

Comments

1

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);

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.