I have a bit of php which has to generate a script. Part of it is pretty much static, but the data is generated on the fly. I had a similar problem in another language a time back and solved it using constant substitutes.
What I'm trying to do:
interface IConstants {
const SUBSTITUTE = '!substitute';
const FULL_STRING = 'var data = "' . self::SUBSTITUTE . '";';
}
class Util {
public static function replace($haystack, $needle, $replace) {
// implementation
}
}
class SampleClass {
public function getScript() {
$someData = $this->getData();
return Util::replace(IConstants::FULL_STRING, IConstants::SUBSTITUTE, $someData);
}
public function getData() {
// generate $someData
return $someData;
}
}
Is this design accepted for PHP? If yes, how would I implement it, and if no, what would be a suitable alternative?