How to access a global $SiteConfiguration variable in PHP from everywhere? - php

I share the PHP code base across all pages and for each HTTP request I dynamically require the "/configs/$site/config.php" file. The file looks like this:
<?php
$SiteConfiguration = [
'site_title => 'Wiki for Developers',
'mysql_host' => 'localhost',
'mysql_db' => 'wiki-devs',
'articles_per_page' => 10,
/* ... etc ... */
];
?>
The problem I'm facing is that I can't quite access this variable from functions.
For example:
function DisplayArticles() {
echo "Displaying ".$SiteConfiguration['articles_per_page'];
}
It will print just Displaying and not Displaying 10.
How can I fix this and have my $SiteConfiguration accessible everywhere? Should I use a class? What's the best practice here?

put
global $SiteConfiguration;
in your function, you can find some more info at http://www.php.net/manual/en/language.variables.scope.php
Since you asked for best practice info: (simplest form)
class MySite{
public static function getConfig(){
return array(
'site_title => 'Wiki for Developers',
'mysql_host' => 'localhost',
'mysql_db' => 'wiki-devs',
'articles_per_page' => 10,
/* ... etc ... */
);
}
}
then in your code you can recall it with
$config = MySite::getConfig();
and use it. (obviously with a better, more descriptive name than MySite ;) )
Advantages:
your php class autoloader will automagically load it for you if setup correctly and your classes can be found, this means not worrying wether you passed a variable, not worrying about function argument placement and not tainting your functions with unnecessary arguments that don't help describe what it does.
you control exactly the access to this data and can make it so that not even your own functions can accidentally change this data, not even when calling other functions that would also need access to it.
in my opinion it beats globals and it beats passing via arguments since it's cleaner and you control the access to it in all forms. You can make certain attributes readonly/writable via specific getter/setter options, keep count of how many times it's accessed and whatever else you can think of.

Here's another case where a class for configuration would work great:
class Config {
private static $site_config = array( 'h' => 'Hello', 'w' => 'World');
public static function get( $key) {
return isset( self::$site_config[$key]) ? self::$site_config[$key] : null;
}
}
echo Config::get( 'h') . ' ' . Config::get( 'w');
This will output: Hello World

use global keyword
function DisplayArticles() {
global $SiteConfiguration;
echo "Displaying ".$SiteConfiguration['articles_per_page'];
}
Edit
You should try to avoid global variable.
A better practice would be to pass your array in parameter
function DisplayArticles( array $config ) {
echo "Displaying ".$config['articles_per_page'];
}
$SiteConfiguration = array( 'site_title' => 'Wiki for Developers',
'mysql_host' => 'localhost',
'mysql_db' => 'wiki-devs',
'articles_per_page' => 10,
/* ... etc ... */
);
DisplayArticles( $SiteConfiguration );

You can try something like this.
Your "siteConfiguration.php" file:
<?php
$SiteConfiguration = [
'site_title' => 'Wiki for Developers',
'mysql_host' => 'localhost',
'mysql_db' => 'wiki-devs',
'articles_per_page' => 10
];
return $SiteConfiguration;
?>
And this function:
function getConfigVar($var) {
static $config = array();
if( empty($config) ) {
$config = require("siteConfiguration.php");
}
return array_key_exists($var, $config) ? $config[$var] : null;
}
This function can also be modified to handle several configs.

Related

pass value for last default parameter of function

From very long time i am working on php.
But one question may I have no idea about
like I have one function as bellow:
function hello($param1, $param2="2", $param3="3", $param4="4")
Now whenever I will use this function and if I need 4th params thats the $param4 then still I need to call all as blank like this one:
hello(1, '', '', "param4");
So is there any another way to just pass 1st and 4th param in call rather then long list of blanks ?
Or is there any other standard way for this ?
There was an RFC for this named skipparams but it was declined.
PHP has no syntactic sugar such as hello(1, , , "param4"); nor hello(1, default, default, "param4"); (per the RFC) for skipping optional parameters when calling a function.
If this is your own function then you can choose the common jQuery style of passing options into plug-ins like this:
function hello( $param1, $more_params = [] )
{
static $default_params = [
'param2' => '2',
'param3' => '3',
'param4' => '4'
];
$more_params = array_merge( $default_params, $more_params );
}
Now you can:
hello( 1, [ 'param4'=>'not 4, muahaha!' ] );
If your function requires some advanced stuff such as type hinting then instead of array_merge() you will need to manually loop $more_params and enforce the types.
One potential way you can do this, while a little bit hacky, may work well in some situations.
Instead of passing multiple variables, pass a single array variable, and inside the function check if the specific keys exist.
function hello($param1, $variables = ["param2" => "2", "param3" => "3", "param4" => "4"]) {
if(!array_key_exists("param2", $variables)) $variables['param2'] = "2";
if(!array_key_exists("param3", $variables)) $variables['param3'] = "3";
if(!array_key_exists("param4", $variables)) $variables['param4'] = "4";
echo "<pre>".print_r($variables, true)."</pre>";
}
This will allow you to set "param4" in the above variable, while still remaining default on all of the others.
Calling the function this way:
hello("test", ["param4" => "filling in variable 4"]);
Will result in the output being:
Array
(
[param4] => filling in variable 4
[param2] => 2
[param3] => 3
)
I don't generally recommend this if it can be avoided, but if you absolutely need this functionality, this may work for you.
The key here is that you have a specifically named index inside the array being passed, that you can check against inside the function itself.
The answer, as I see it, is yes and no.
No, because there's no way to do this in a standard fashion.
Yes, because you can hack around it. This is hacky, but it works ;)
Example:
function some_call($parm1, $parm2='', $parm3='', $parm4='') { ... }
and the sauce:
function some_call_4($parm1, $parm4) {
return some_call($parm1, '', '', $parm4);
}
So if you make that call ALOT and are tired of typing it out, you can just hack around it.
Sorry, that's all I've got for you.
It is an overhead, but you can use ReflectionFunction to create a class, instance of which that can be invoked with named parameters:
final class FunctionWithNamedParams
{
private $func;
public function __construct($func)
{
$this->func = $func;
}
public function __invoke($params = [])
{
return ($this->func)(...$this->resolveParams($params));
}
private function resolveParams($params)
{
$rf = new ReflectionFunction($this->func);
return array_reduce(
$rf->getParameters(),
function ($carry, $param) use ($params) {
if (isset($params[$param->getName()])) {
$carry[] = $params[$param->getName()];
} else if ($param->isDefaultValueAvailable()) {
$carry[] = $param->getDefaultValue();
} else {
throw new BadFunctionCallException;
}
return $carry;
},
[]
);
}
}
Then you can use it like this:
function hello($param1, $param2 = "2", $param3 = "3", $param4 = "4")
{
var_dump($param1, $param2, $param3, $param4);
}
$func = new FunctionWithNamedParams('hello');
$func(['param1' => '1', 'param4' => 'foo']);
Here is the demo.

How to define hardcoded array data as part of a PHP framework?

For example the 50 united states. Right now I use the 50 states in only a single function:
function getStateInput() {
$states = array('AL' => 'Alabama', 'AK' => 'Alaska',
'AZ' => 'Arizona', etc);
//use $states
}
Alternatively you could define $states in its own states.php and then use it like this:
function getStateInput() {
include('states.php');
//use $states
}
But for some reason that scares me because I am using an include to define a local function variable. Another way would be to assign a states array to a superglobal in a states.php (for example I could use $_ENV['states']). Then I could go:
include('states.php');
function getStateInput() {
//use $_ENV['states']
}
Is one of those best or is there another better way?
The common way is to separate concerns. You can eg. have your own class with address-related methods. This class would be the natural place to store information about possible states, like that:
class Address {
static STATES = array(
'AL' => 'Alabama',
'AK' => 'Alaska',
'AZ' => 'Arizona',
// ...
);
}
// and then you can use it somewhere like that:
echo Address::$STATES['AL'];
Why not just have the include file have a function that returns the state array. Then you use that array in your other code. That way you don't deal with any scope variables.
// states.php
function getStates() {
return $states;
}
// other.php
function getStateInput() {
include('states.php');
$states = getStates();
// Do stuff with $states
}
In your include.php file (you have to include this file. Include it before your function declaration):
$states = array('GA' => 'Georgia', 'SC' => 'South Carolina');
On your script:
include("include.php");
global $states;
function GetStateInput() {
///States are already available as $states
}
Good luck!
I would probably do it like this
include.php
<?php
$array = array();
$array['foo'] = 'bar';
$array['oof'] = 'rab';
... etc.
return $array;
?>
framework.php
<?php
// previous stuff
function getStateInput()
{
static $a = NULL;
if($a === NULL)
$a = include('include.php');
// do whatever
return $a;
}
// other stuff
?>
Either that, or just define the array in the function. It would depend on how big the array is and how big the rest of the api. If maintenance gets a lot easier moving the array into an external file - do it. If you use the array in multiple different functions or methods, pull it out. etc. You know the drill.
PHP optimizes file includes, so don't worry about performance too much if you don't include dynamically.

How to match new variables against default variables in a class?

In jQuery there is $.extend() function. That basically works like matching default settings with input settings. Well, I want to use similar technique in PHP, but it seems, that there is no similar function. So I'm wondering: Is there a similar function as .extend(), but in PHP? If not then, what alternatives are there?
This is an example class and how I get this effect currently. I also added a comment, how I would wish to do this:
class TestClass {
// These are the default settings:
var $settings = array(
'show' => 10,
'phrase' => 'Miley rocks!',
'by' => 'Kalle H. Väravas',
'version' => '1.0'
);
function __construct ($input_settings) {
// This is how I would wish to do this:
// $this->settings = extend($this->settings, $input_settings);
// This is what I want to get rid of:
$this->settings['show'] = empty($input_settings['show']) ? $this->settings['show'] : $input_settings['show'];
$this->settings['phrase'] = empty($input_settings['phrase']) ? $this->settings['phrase'] : $input_settings['phrase'];
$this->settings['by'] = empty($input_settings['by']) ? $this->settings['by'] : $input_settings['by'];
$this->settings['version'] = empty($input_settings['version']) ? $this->settings['version'] : $input_settings['version'];
// Obviously I cannot do anything neat or dynamical with $input_settings['totally_new'] :/
}
function Display () {
// For simplifying purposes, lets use Display and not print_r the settings from construct
return $this->settings;
}
}
$new_settings = array(
'show' => 30,
'by' => 'Your name',
'totally_new' => 'setting'
);
$TC = new TestClass($new_settings);
echo '<pre>'; print_r($TC->Display()); echo '</pre>';
If you notice, that there is a totally new setting: $new_settings['totally_new']. That should just get included inside the array as $this->settings['totally_new']. PS: Above code outputs this.
Try to use array_merge php function. Code:
array_merge($this->settings, $input_settings);

Converting code with Anonymous functions to PHP 5.2

I have some PHP 5.3 code which builds an array to be passed to a view. This is the code I have.
# Select all this users links.
$data = $this->link_model->select_user_id($this->user->id);
if (count($data) > 0) {
# Process the data into the table format.
$table = array
(
'properties' => array
(
'delete_link_column' => 0,
),
'callbacks' => array
(
# Callback for the name link.
function($value) {
return sprintf('%s', $value, $value);
},
# Callback for the category link.
function($value) {
return sprintf('%s', $value, $value);
},
# Callback for the creation date.
function($value) {
return date('jS M Y', $value);
},
# Callback for the delete link.
function($value) {
return sprintf('delete', $value);
},
),
'columns' => array
(
'name', 'category', 'creation date',
),
'data' => array
(
),
'sorting' => array
(
'sort' => false,
),
);
However the problem is that I cannot use anonymous functions in PHP 5.2, which is the server I must upload this schoolwork. The view requires callback functions to be defined so it can call them.
What would be the neatest way to convert this PHP code to not using anonymous functions? Thanks.
You could call one of those function like so:
$func = $callbacks[0];
$func();
Which also works with create_function() and using strings for named functions like so:
function test() {
echo "test";
}
$func = 'test';
$func();
$func = create_function('' , 'echo "test 2"; ');
$func();
Also, if the calling is done using call_user_func you can use array($object, 'func_name') to call a public method on an object or a class with array('Class_Name', 'func_name'):
class Test {
public static function staticTest() { echo "Static Test"; }
public function methodTest() { echo "Test"; }
public function runTests() {
$test = array('Test', 'staticTest');
call_user_func($test);
$test = array($this, 'methodTest');
call_user_func($test);
}
}
$test = new Test();
$test->runTests();
Anonymous functions are great for ephemeral one-offs, like event listeners in patterns like Observer.
However, since you've already formalized an interface (callbacks for rendering names, categories, creation dates, and a delete link) you may as well go the extra step of defining a 'renderer' interface that requires those 4 methods to be implemented. Instead of passing callbacks you'd pass a single renderer subclass to the view, which could then be used to call the appropriate methods. The view could also validate it by checking the parent class. That would still allow you to swap renderers in the spirit of portable, reusable OOP without requiring anonymous functions.
Is there a situation where your callbacks would ever be coming from arbitrary code (e.g. plugins)? If not, there's really no benefit to making those callbacks anonymous. It might seem like you're saving a little namespace bloat, but you're also making it tougher to debug or document.

PHP Object Validation

I'm currently working on an OO PHP application. I have a class called validation which I would like to use to check all of the data submitted is valid, however I obviously need somewhere to define the rules for each property to be checked. At the moment, I'm using arrays during the construction of a new object. eg:
$this->name = array(
'maxlength' => 10,
'minlength' => 2,
'required' => true,
'value' => $namefromparameter
)
One array for each property.
I would then call a static method from the validation class which would carry out various checks depending on the values defined in each array.
Is there a more efficient way of doing this?
Any advice appreciated.
Thanks.
I know the associative array is used commonly to configure things in PHP (it's called magic container pattern and is considered bad practice, btw), but why don't you create multiple validator classes instead, each of which able to handle one rule? Something like this:
interface IValidator {
public function validate($value);
}
$validators[] = new StringLengthValidator(2, 10);
$validators[] = new NotNollValidator();
$validators[] = new UsernameDoesNotExistValidator();
This has multiple advantages over the implementation using arrays:
You can document them (very important), phpdoc cannot parse comments for array keys.
Your code becomes typo-safe (array('reqiured' => true))
It is fully OO and does not introduce new concepts
It is more readable (although much more verbose)
The implementation of each constraint can be found intuitively (it's not in a 400-line function, but in the proper class)
EDIT: Here is a link to an answer I gave to a different question, but that is mostly applicable to this one as well.
Since using OO it would be cleaner if you used classes for validating properties. E.g.
class StringProperty
{
public $maxLength;
public $minlength;
public $required;
public $value;
function __construct($value,$maxLength,$minLength,$required)
{
$this->value = $value;
$this-> maxLength = $maxLength;
$this-> minLength = $minLength;
$this-> required = $required;
}
function isValidat()
{
// Check if it is valid
}
function getValidationErrorMessage()
{
}
}
$this->name = new StringProperty($namefromparameter,10,2,true);
if(!$this->name->isValid())
{
$validationMessage = $this->name-getValidationErrorMessage();
}
Using a class has the advantage of encapsulating logic inside of it that the array (basically a structure) does not have.
Maybe get inspired by Zend-Framework Validation.
So define a master:
class BaseValidator {
protected $msgs = array();
protected $params = array();
abstract function isValid($value);
public function __CONSTRUCT($_params) {
$this->params = $_params;
}
public function getMessages() {
// returns errors-messages
return $this->msgs;
}
}
And then build your custom validators:
class EmailValidator extends BaseValidator {
public function isValid($val=null) {
// if no value set use the params['value']
if ($val==null) {
$val = $this->params['value'];
}
// validate the value
if (strlen($val) < $this->params['maxlength']) {
$this->msgs[] = 'Length too short';
}
return count($this->msgs) > 0 ? false : true;
}
}
Finally your inital array could become something like:
$this->name = new EmailValidator(
array(
'maxlength' => 10,
'minlength' => 2,
'required' => true,
'value' => $namefromparameter,
),
),
);
validation could then be done like this:
if ($this->name->isValid()) {
echo 'everything fine';
} else {
echo 'Error: '.implode('<br/>', $this->name->getMessages());
}

Categories