PHP how to set static variable from a string - php

Is it possible to be able to dynamically set a php static class variable? In the example below i want to pass a string representation of the variable i want to set to a function "databaseInit" which then sets the var...
class app {
static $database_smf;
static $database_phpbb;
/**
* Initialise the app
*/
static function init(){
// No initialise the db connection
self::databaseInit('database_phpbb');
self::databaseInit('database_smf');
}
static function databaseInit( $database ){
// is it possible to dynamically set the static var based on the param provided? eg:
self::[$database] = true;
}
}

You can use a plain variable variable name:
static function databaseInit( $database ){
self::$$database = true;
}
.. but you should really rework it to just keep an array and manipulate the keys of the array instead, as that will keep all the settings inside a single namespace instead of potentially doing weird stuff with other static variables if a name is mistyped, etc.
class app {
static $databases = [];
...
static function databaseInit($database) {
self::$databases[$database] = true;
}
}
And the next step would be to make the class non-static instead, so it can be easier tested and will keep its state locally instead.

Yes that is possible.
Just a little change in your code:
Use:
self::$$database = true;
Instead of:
self::[$database] = true;
class app {
static $database_smf;
static $database_phpbb;
Full code:
/**
* Initialise the app
*/
static function init(){
// No initialise the db connection
self::databaseInit('database_phpbb');
self::databaseInit('database_smf');
}
static function databaseInit( $database ){
// is it possible to dynamically set the static var based on the param provided? eg:
self::$$database = true;
}
}

Related

How to run code from included filepath of object into function scope outside object?

I am currently refactoring code from a page parser function to OOP.
I am having difficulties including and running code from a file into main function scope:
Object:
class phpFragment {
private $sData;
function render() {
return include $oElement->sData;
}
}
Object container class:
class pageData {
protected $aPhpFragments;
protected $aCssFragments;
public function outputData($sTag) {
switch($sTag) {
case 'php':
foreach($this->aPhpFragments as $oPhpFragment) {
return $oPhpFragment->render();
}
break;
case 'css':
foreach($this->aCssFragments as $oCssFragment) {
echo $oCssFragment->render();
}
break;
}
}
}
Main function:
function parsePage($sLanguageCode) {
$oTranslator = new translator($sLanguageCode);
$aTranslations = $oTranslator->translations('page');
$oBuilderClass = new builder($aTranslations);
//... queries to get data and set pagedata and get the template file
$oPageData = $oPage->getData();
$aTemplateTags = $oTemplate->getTags();
foreach($aTemplateTags as $sTag) {
$oPageData->outputData($sTag);
}
//....
}
Code of include (example):
<?php
$oBuilderClass->build_element(.... parameters here);
?>
I want to initiate the builder-class only once, because it contains quite some data and I don't want to recreate that on every include.
How can I return the code of the include into the parsePage function where the builderClass can be used?
You can create a Context class that will be a container of your scope variables and helps you include (execute) code inside a context. It will be a singleton class (only one instance will be created).
Here is how to use it: The method current() returns the current instance then you can export variables to the context by using the export() method, it takes a key/value array. The method execute() takes a file name as a parameter and includes it with the exported variables available, you can add temporary variables as a second parameter:
//Somewhere before execute();
oContext::current()->export([
'variable1' => 'value1',
'instance' => $instance
]);
//Then anywhere in your file:
oContext::current()->execute("toBeIncluded.php", [
'tmp_variable' => 'tmp_value'
]);
//toBeIncluded.php
echo $variable1;
echo $instance->method1();
echo $tmp_variable;
In your case:
Main function:
function parsePage($sLanguageCode) {
$oTranslator = new translator($sLanguageCode);
$aTranslations = $oTranslator->translations('page');
$oBuilderClass = new builder($aTranslations);
//export variables to your context
//Don't be aware of memroy usage objects are passed by reference
oContext::current()->export(compact('oBuilderClass'));
//... queries to get data and set pagedata and get the template file
$oPageData = $oPage->getData();
$aTemplateTags = $oTemplate->getTags();
foreach($aTemplateTags as $sTag) {
$oPageData->outputData($sTag);
}
//....
}
Object:
class phpFragment {
private $sData;
function render() {
oContext::current()->execute($oElement->sData);
}
}
You find bellow the class declaration:
oContext.class.php
/**
* Class oContext
*/
class oContext {
/**
* The singleton instance
* #var oContext
*/
private static $instance = null;
/**
* the exported variables
* #var array
*/
private $variables = [];
/**
* Return the singleton or create one if does not exist
*
* #return oContext
*/
public static function current() {
if (!self::$instance) {
self::$instance = new self;
}
return self::$instance;
}
/**
* Export an array of key/value variables
*
* #param $variables
* #return $this
*/
public function export($variables) {
foreach ($variables as $key => $value) {
$this->variables[$key] = $value;
}
return $this;
}
/**
* Include and execute a file in this context
*
* #param $file
* #param array $variables temporary exports will not be added to the context (not available in the next call)
* #return $this
*/
public function execute($file, $variables = []) {
//Populate variables
foreach (array_merge($this->variables, $variables) as $key => $value) {
${$key} = $value;
}
include $file;
return $this;
}
}
I hope this help you achieve your aim.
Good Luck.
If I correctly understand your problem then you want to execute a whole code from php file as a method called from object. If yes then you probably want to use a eval function described here.
With eval function you can read your php file as a string and evaluate it as php code instead of including it.
If your php file use a return statement then following by documentation
eval() returns NULL unless return is called in the evaluated code, in
which case the value passed to return is returned.
you can simply return that value from your method.
If your included files are as simple as you show in example then to achieve this effect you need to replace this part of your code
class phpFragment {
private $sData;
function render() {
return include $oElement->sData;
}
}
with this
class phpFragment {
private $sData;
function render() {
//read a file into variable as string
$phpCode = file_get_contents($oElement->sData);
//prepare code by adding return statement and '?>' at the begining (because you have an open tag in php files).
$phpCode = '?> ' . str_replace('$oBuilderClass->build_element', 'return $oBuilderClass->build_element', $phpCode);
//I guess that included files don't use any variables declared out of file so we need to simply escape every '$' character in file
//that they can evaluate correctly.
$phpCode = str_replace('$', '\$', $phpCode);
return eval($phpCode);
}
}
Sounds like a dependency injection problem: you want $oBuilderClass to be in scope inside the include code?
If you have access to an application dependency container, I'd register the object with that container. In generic terms, something like \Application::bind('Builder', $oBuilderClass), then later do Builder::build_element. However, that you are writing your own view renderer suggests you don't have access to a framework facility with a formal IoC container.
Supposing you don't have an IoC container, the most expedient way would be to do:
$GLOBALS['oBuilderClass'] = new builder(...);
then later in your include:
global $oBuilderClass;
$oBuilderClass->build_element(...);
This is not particularly elegant, however. You might consider passing the builder around, so that at the bottom of the call well you have:
function render(builder $oBuilderClass) {
return include $oElement->sData;
}
which puts $oBuilderClass in scope at the time of the include. I would prefer a formal IoC container first, then passing the object around, then finally if none of these work for you, then using the global variable.

PHP select from DB and mantain the values without selecting again

I have a specific table in the DB that contains some static data.
This data is anyway required by many methods and each method calls the DB again and again to grab this data.
Now is there a way in PHP to select the data from the DB and mantain it as it was a constant or a SESSION ( I cannot use sessions in this case ) ?
What I am trying to do is to put the request in the contruct and to make the variable static, but it does not change the result. Each time a method calls the static variable, the select in the DB is done anyway..
class service {
public static $actions;
public function __construct() {
self::$actions = self::getActions();
}
public static function getActions() {
$actions = self::$db->select('_actions', '*');
return $actions;
}
}
Your code is already pretty close. You just need to add a check to see if the data has already been queried, and make sure you're using the static class variable, not a local variable.
You should realize that static variables and constructors live in two different worlds. Static variables are persistent for the lifetime of the class - constructors run once per instance.
public static $actions = null;
public static function getActions() {
if (self::$actions === null) {
self::$actions = self::$db->select('_actions', '*');
}
return self::$actions;
}
Look into caching the data - http://www.phpfastcache.com/
You can use a library like the link above or write something simple that serializes the data and writes it to a file. If the file exists then use that data and deserialize it on subsequent requests for the same data set.
Similar to Sam Dufel's answer, but do the check and query in the constructor.
class service {
public static $actions;
public function __construct() {
if (!self::$actions) {
self::$actions = self::$db->select('_actions', '*');
}
}
public static function getActions() {
return self::$actions;
}
}

Change class, private or public string

There is a php class in which I like to change the variable, but I can't make it happen.
The Class:
class ShopCart
{
private $maincurrency = 'USD';
private function set_Currency() {
$maincurrency = 'GBP';
return $maincurrency;
}
}
This doesn't work. Even if I make it public. What am I doing wrong?
You have to use $this->maincurrency in the body of your method. Your current code creates and sets local variable, not a member.
You need $this:
class ShopCart {
private $maincurrency = 'USD';
private function set_Currency() {
$this->$maincurrency = 'GBP';
return $this->$maincurrency;
}
}
Otherwise you're creating a new variable local to the function and just using that.
The manual does actually tell you this already:
Within class methods the properties, constants, and methods may be
accessed by using the form $this->property (where property is the name
of the property) unless the access is to a static property within the
context of a static class method, in which case it is accessed using
the form self::$property.
The documentation is your friend; consult it before asking here please.
You may also want to consider not returning anything from a setter, though that's up to you. There are advantages to doing so, even if it's not all that conventional in PHP.
class ShopCart {
private $maincurrency = 'USD';
private function setCurrency() {
$this->maincurrency = 'GBP';
}
}
change $maincurrency = 'GBP'; to $this->maincurrency = 'GBP';,
and also return $this->maincurrency;

Accessing a global array inside class constructor

My aim is to retrieve some data from a global array which is defined in another PHP file. My code is running inside database.php file and the array I want to use is inside config.php file. I understand that accessing a global array inside class is not a good idea, but I want to do it so because of some reasons.
My code is as below:
config.php
$CONFIG = array();
// ...
$CONFIG["DATABASE"] = array();
$CONFIG["DATABASE"]["USERNAME"] = "user";
$CONFIG["DATABASE"]["PASSWORD"] = "pass";
$CONFIG["DATABASE"]["HOSTNAME"] = "127.0.0.1";
$CONFIG["DATABASE"]["DATABASE"] = "my_db";
// ...
database.php
require('config.php');
class Database
{
protected $m_Link;
private $m_User;
private $m_Pass;
private $m_Host;
private $m_Data;
private $m_bConnected;
public function __construct()
{
global $CONFIG;
$this->m_User = $CONFIG["DATABASE"]["USERNAME"];
$this->m_Pass = $CONFIG["DATABASE"]["PASSWORD"];
$this->m_Host = $CONFIG["DATABASE"]["HOSTNAME"];
$this->m_Data = $CONFIG["DATABASE"]["DATABASE"];
$this->m_bConnected = false;
$this->Connect();
}
// ...
};
There is no error given (except for the failed database connection notification).
I can't access to the array elements. For instance $CONFIG["DATABASE"]["USERNAME"] returns an empty value even though it was initialized with "user" string in the config.php.
How should I modify my code so that it can this global array can be accessible inside the class constructor?
(Note: PHP version is 5.3.0)
Your code looks right, so I think you should just debug it. Try to output $CONFIG before creating instance of Database class, $CONFIG may be redefined/changed somewhere in your code. And don't just check one value in array - output whole array with var_dump/print_r.
Instead of
$CONFIG = array();
use
$GLOBALS['CONFIG'] = array();
I think some how global is not working in __construct(). I am not sure if it is a bug or it is designed as it is.
For code
<?php
class Test {
public $value;
function __construct() {
global $value;
$value = "I am a test.";
}
}
$test = new Test();
echo $test->value;
You will see nothing when above php runs.
However, if you do not use global, but use $this->value, everything works fine.
<?php
class Test {
public $value;
function __construct() {
$this->value = "I am a test.";
}
}
$test = new Test();
echo $test->value;
If you insist to get a reason. I think maybe __construct() is design to initialize the properties. Some code like $this->value = $value is using a lot in __construct(). So maybe the php designer thinks it is not good practice to use global in __construct(). However. I can not find a word mentioned it in php manual after all.

Static class initializer in PHP

I have an helper class with some static functions. All the functions in the class require a ‘heavy’ initialization function to run once (as if it were a constructor).
Is there a good practice for achieving this?
The only thing I thought of was calling an init function, and breaking its flow if it has already run once (using a static $initialized var). The problem is that I need to call it on every one of the class’s functions.
Sounds like you'd be better served by a singleton rather than a bunch of static methods
class Singleton
{
/**
*
* #var Singleton
*/
private static $instance;
private function __construct()
{
// Your "heavy" initialization stuff here
}
public static function getInstance()
{
if ( is_null( self::$instance ) )
{
self::$instance = new self();
}
return self::$instance;
}
public function someMethod1()
{
// whatever
}
public function someMethod2()
{
// whatever
}
}
And then, in usage
// As opposed to this
Singleton::someMethod1();
// You'd do this
Singleton::getInstance()->someMethod1();
// file Foo.php
class Foo
{
static function init() { /* ... */ }
}
Foo::init();
This way, the initialization happens when the class file is included. You can make sure this only happens when necessary (and only once) by using autoloading.
Actually, I use a public static method __init__() on my static classes that require initialization (or at least need to execute some code). Then, in my autoloader, when it loads a class it checks is_callable($class, '__init__'). If it is, it calls that method. Quick, simple and effective...
NOTE: This is exactly what OP said they did. (But didn't show code for.) I show the details here, so that you can compare it to the accepted answer. My point is that OP's original instinct was, IMHO, better than the answer he accepted.
Given how highly upvoted the accepted answer is, I'd like to point out the "naive" answer to one-time initialization of static methods, is hardly more code than that implementation of Singleton -- and has an essential advantage.
final class MyClass {
public static function someMethod1() {
MyClass::init();
// whatever
}
public static function someMethod2() {
MyClass::init();
// whatever
}
private static $didInit = false;
private static function init() {
if (!self::$didInit) {
self::$didInit = true;
// one-time init code.
}
}
// private, so can't create an instance.
private function __construct() {
// Nothing to do - there are no instances.
}
}
The advantage of this approach, is that you get to call with the straightforward static function syntax:
MyClass::someMethod1();
Contrast it to the calls required by the accepted answer:
MyClass::getInstance->someMethod1();
As a general principle, it is best to pay the coding price once, when you code a class, to keep callers simpler.
If you are NOT using PHP 7.4's opcode.cache, then use Victor Nicollet's answer. Simple. No extra coding required. No "advanced" coding to understand. (I recommend including FrancescoMM's comment, to make sure "init" will never execute twice.) See Szczepan's explanation of why Victor's technique won't work with opcode.cache.
If you ARE using opcode.cache, then AFAIK my answer is as clean as you can get. The cost is simply adding the line MyClass::init(); at start of every public method. NOTE: If you want public properties, code them as a get / set pair of methods, so that you have a place to add that init call.
(Private members do NOT need that init call, as they are not reachable from the outside - so some public method has already been called, by the time execution reaches the private member.)
There is a way to call the init() method once and forbid it's usage, you can turn the function into private initializer and ivoke it after class declaration like this:
class Example {
private static function init() {
// do whatever needed for class initialization
}
}
(static function () {
static::init();
})->bindTo(null, Example::class)();
I am posting this as an answer because this is very important as of PHP 7.4.
The opcache.preload mechanism of PHP 7.4 makes it possible to preload opcodes for classes. If you use it to preload a file that contains a class definition and some side effects, then classes defined in that file will "exist" for all subsequent scripts executed by this FPM server and its workers, but the side effects will not be in effect, and the autoloader will not require the file containing them because the class already "exists". This completely defeats any and all static initialization techniques that rely on executing top-level code in the file that contains the class definition.
If you don't like public static initializer, reflection can be a workaround.
<?php
class LanguageUtility
{
public static function initializeClass($class)
{
try
{
// Get a static method named 'initialize'. If not found,
// ReflectionMethod() will throw a ReflectionException.
$ref = new \ReflectionMethod($class, 'initialize');
// The 'initialize' method is probably 'private'.
// Make it accessible before calling 'invoke'.
// Note that 'setAccessible' is not available
// before PHP version 5.3.2.
$ref->setAccessible(true);
// Execute the 'initialize' method.
$ref->invoke(null);
}
catch (Exception $e)
{
}
}
}
class MyClass
{
private static function initialize()
{
}
}
LanguageUtility::initializeClass('MyClass');
?>
Some tests of assigning static public properties :
settings.json :
{
"HOST": "website.com",
"NB_FOR_PAGINA": 8,
"DEF_ARR_SIZES": {
"min": 600,
"max": 1200
},
"TOKEN_TIME": 3600,
"WEBSITE_TITLE": "My website title"
}
now we want to add settings public static properties to our class
class test {
/** prepare an array to store datas */
public static $datas = array();
/**
* test::init();
*/
public static function init(){
// get json file to init.
$get_json_settings =
file_get_contents(dirname(__DIR__).'/API/settings.json');
$SETTINGS = json_decode($get_json_settings, true);
foreach( $SETTINGS as $key => $value ){
// set public static properties
self::$datas[$key] = $value;
}
}
/**
*
*/
/**
* test::get_static_properties($class_name);
*
* #param {type} $class_name
* #return {log} return all static properties of API object
*/
public static function get_static_properties($class_name) {
$class = new ReflectionClass($class_name);
echo '<b>infos Class : '.$class->name.'</b><br>';
$staticMembers = $class->getStaticProperties();
foreach( $staticMembers as $key => $value ){
echo '<pre>';
echo $key. ' -> ';
if( is_array($value) ){
var_export($value);
}
else if( is_bool($value) ){
var_export($value);
}
else{
echo $value;
}
echo '</pre>';
}
// end foreach
}
/**
* END test::get_static_properties();
*/
}
// end class test
ok now we test this code :
// consider we have the class test in API folder
spl_autoload_register(function ($class){
// call path to API folder after
$path_API = dirname(__DIR__).'/API/' . $class . '.php';
if( file_exists($path_API) ) require $path_API;
});
// end SPL auto registrer
// init class test with dynamics static properties
test::init();
test::get_static_properties('test');
var_dump(test::$HOST);
var_dump(test::$datas['HOST']);
this return :
infos Class : test
datas -> array (
'HOST' => 'website.com',
'NB_FOR_PAGINA' => 8,
'DEF_ARR_SIZES' =>
array (
'min' => 600,
'max' => 1200,
),
'TOKEN_TIME' => 3600,
'WEBSITE_TITLE' => 'My website title'
)
// var_dump(test::$HOST);
Uncaught Error: Access to undeclared static property:
test::$HOST
// var_dump(test::$datas['HOST']);
website.com
Then if we modify the class test like this :
class test {
/** Determine empty public static properties */
public static $HOST;
public static $NB_FOR_PAGINA;
public static $DEF_ARR_SIZES;
public static $TOKEN_TIME;
public static $WEBSITE_TITLE;
/**
* test::init();
*/
public static function init(){
// get json file to init.
$get_json_settings =
file_get_contents(dirname(__DIR__).'/API/settings.json');
$SETTINGS = json_decode($get_json_settings, true);
foreach( $SETTINGS as $key => $value ){
// set public static properties
self::${$key} = $value;
}
}
/**
*
*/
...
}
// end class test
// init class test with dynamics static properties
test::init();
test::get_static_properties('test');
var_dump(test::$HOST);
this return :
infos Class : test
HOST -> website.com
NB_FOR_PAGINA -> 8
DEF_ARR_SIZES -> array (
'min' => 600,
'max' => 1200,
)
TOKEN_TIME -> 3600
WEBSITE_TITLE -> My website title
// var_dump(test::$HOST);
website.com
I actually need to initialize an object with public static properties that I will reuse in many other classes, which I think is supposed to, I don't want to do new api() in every method where I would need, for example to check the host of the site or indicate it. Also I would like to make things more dynamic so that I can add as many settings as I want to my API, without having to declare them in my initialization class.
All other methods I've seen no longer work under php > 7.4
I keep looking for a solution for this problem.
Note - the RFC proposing this is still in the draft state.
class Singleton
{
private static function __static()
{
//...
}
//...
}
proposed for PHP 7.x (see https://wiki.php.net/rfc/static_class_constructor )

Categories