0

I don't even know if this is possible but I'm trying to set an optional value to an existing object.

Here is a simplified version of the code I'm trying.

<?php

class configObject {

private $dataContainer = array();

public function set($dataKey, $dataValue) {
  $this->dataContainer[$dataKey] = $dataValue;
  return TRUE;
}

public function get($dataKey) {
  return $this->dataContainer($dataKey);
}

$this->set('someValue', 'foobar');

} //End configObject Class

function getPaginationHTML($c = &$_config) {

  $someOption = $c->get('someValue');
  // Do other stuff
  return $html;
}

$_config = new configObject();

$html = getPaginationHTML();


?>

I'm getting the error: syntax error, unexpected '&' in

Any help is appreciated, again I'm not sure if it's even possible to do what I'm trying to do so sorry for being a noob.

Thanks

2
  • Since getPaginationHTML seems to be relative to your class why don't you define it as a method of the class? You can use the decorator pattern too. Commented Feb 1, 2015 at 10:27
  • Can you post an answer using the decorator pattern? I don't want to add it to the configObject class because it only contains config info but if I can extend the class or something. BTW I am using a dependency container for injection on config, database, session classes in my app. Commented Feb 1, 2015 at 10:42

1 Answer 1

1

example with the decorator pattern:

class ConfigObject {

    private $dataContainer = array();

    public function set($dataKey, $dataValue) {
        $this->dataContainer[$dataKey] = $dataValue;
        return true;
    }

    public function get($dataKey) {
        return $this->dataContainer[$dataKey];
    }

}

class ConfigObjectDecorator {
    private $_decorated;


    public function __construct($pDecorated) {
        $this->_decorated = $pDecorated;
    }

    public function getPaginationHTML($dataKey) {
        $someOption = $this->get($dataKey);
        // Do other stuff
        $html = '<p>' . $someOption . '</p>';
        return $html;
    }

    public function set($dataKey, $dataValue) {
        return $this->_decorated->set($dataKey, $dataValue);    
    }

    public function get($dataKey) {
        return $this->_decorated->get($dataKey);    
    }
}

class ConfigFactory {
    public static function create () {
        $config = new ConfigObject();
        return new ConfigObjectDecorator($config);
    }
}

$config = ConfigFactory::create();
if ($config->set('mykey', 'myvalue'))
    echo $config->getPaginationHTML('mykey');

Note that can easily rewrite ConfigFactory::create() to add a parameter to deals with other types of decoration (or none).

Sign up to request clarification or add additional context in comments.

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.