PHP Classes

PHP common data gateway: Set and get values of common data structures

Recommend this page to a friend!
  Info   View files Example   View files View files (5)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 271 All time: 7,697 This week: 116Up
Version License PHP version Categories
common-gateway 1.0BSD License5.3PHP 5, Data types
Description 

Author

This package can set and get values of common data structures.

It provides static functions that can be called from anywhere to setup a common data structure with a name and values for named properties. A data structure is created if one does not exist with the given name.

Properties may be changed to a given value specifying a string with the name of the data structure followed by the name of the property separated by a dot. If no value is passed, the current value of the property is returned.

Picture of Vallo Reima
  Performance   Level  
Name: Vallo Reima <contact>
Classes: 6 packages by
Country: Estonia Estonia
Innovation award
Innovation award
Nominee: 3x

Example

<?php

/* demonstrating of common gateway */

include 'gateway.php';
include
'common.php';

define('BR', '<br />');

/* test data */
$ctt = array('phn' => '444555666', 'eml' => 'info@mail.com',
           
'adr' => (object) array('str' => 'Elm', 'twn' => 'Sunny'));

¤::_Init(); /* instantiate common */
¤::Startup(); /* call a common method */

¤::_('ctt', $ctt); /* set a structured value */
¤::_('ctt.chd', array('Sally', 'Billy')); /* set an array value */
¤::_('bye', 'thnk'); /* save a value in workarea */

header("Content-Type: text/html; charset=utf-8");
echo
¤::_('msg')->titl . BR; /* get a common object's property value */
echo 'Phone: ' . ¤::_('ctt.phn') . BR; /* get a value */
¤::_('ctt.adr.str', 'Oak'); /* replace a value */
echo 'Street: ' . ¤::_('ctt.adr.str') . BR; /* get a value */
echo 'Town: ' . ¤::_('ctt.adr.twn') . BR; /* get a value */
$c = 'chd';
if (
version_compare(PHP_VERSION, '5.4', '<')) {
 
$a = ¤::_("ctt.$c"); /* get a value */
 
echo 'Child1: ' . $a[0] . BR;
} else {
  echo
'Child1: ' . ¤::_("ctt.$c")[0] . BR; /* array dereferencing */
}

Bye();

function
Bye() { /* access common data from local scope */
 
$c = ¤::_('bye'); /* retrieve a value from workarea */
 
$m = ¤::_('msg')->$c; /* get a value of common object's property */
 
exit($m);
}


Details

PHP common data gateway

There is a way described here of how to organize common data to be available throughout a program. Common data (also called Global data) in this context are the functions and variables (or methods and properties) accessed during one server request. Permanent data saved between sessions are not covered here. In larger projects, it becomes more important how to manage common data to be easily reachable in any part of the program.

How it works

All the common accessing is collected into one point. Thru this point - a gateway - all the global usings can be controlled and observed. The common gateway is realized by two classes: the gateway itself, containing some static properties and methods, and the common data class, holding common methods and properties. Common methods are called from the gateway via __callStatic() magic method. Common properties are accessed via property path methods. Common work area is supplied also for temporary and/or time-critical data. The methods do not check for errors - it's presumed to be an error handling responsibility.

Since the gateway can be referred many times in the program then the names concerning a gateway are selected to be short.

  • The '¤' as well-distinguishable is used for a gateway classname and '_' to access common properties.
  • The gateway is activated by ¤::_Init() method (see example.php).
  • The common methods are called simply using the ¤:: prefix, for example: ¤::Startup(), ¤::Convert($txt) etc.
  • The properties are accessed via ¤::_() method having 1st argument as a property path string.
  • The path separator is '.' by default and can be changed.
  • If 2nd argument is missing, a property value is gotten, for example ¤::_('addr.street').
  • Otherwise new value is assigned to a property, for example ¤::_('addr.street','Elm').
  • The property path member must be either associative array key or object element name.
  • Only path's terminal value can be overloaded by get/set magic methods;
  • The workarea is accessible via ¤::$_ public property.

Note. A more rough way would be to implement all the common properties via ¤::$_ but it makes properties vulnerable for occasional over-writings.

How it fits OOP

Common/Global state is unavoidable when you work with any information, we should not disregard it, but instead we need to decide how to work with it. PHP core functions must be extended by customer ones to perform standard/elemantary actions. Collecting them into one class/object instead of scattering into many classes achieves more comprehensiveness. Certain data (database link, text arrays, configurations etc.), once retrieved or initialized, need to be easily reachable in any part of the project code. Making them centrally accessible releases from a pain to pass frequently-used variables continuously throughout several objects/methods/functions.

The design patterns like DI container, Service Locator, Singletone all allow the same thing - make some data centrally available. The gateway approach is a solution following OOP paradigm as far as possible. Developer is responsible for decision - what is a subject to class or common method/property. The gateway class can be complemented with several functionality to have more control over common data.

The package

The following files are included:

  1. gateway.php - the gateway class; requires PHP 5.3+;
  2. common.php - the common data class skeleton; contains necessary methods and properties for the gateway class, and some data for the example;
  3. example.php - demonstrates the common gateway functionality.

The common data gateway is implemented in vRegistry solutions (see [vregistry.com]).

ChangeLog

20 June 2015

  • common.php - _get/_set methods to allow associative array keys in the property path

07 October 2016

  • common.php - corrections for the php7 compatibility

    [vregistry.com]: http://vregistry.com


  Files folder image Files  
File Role Description
Plain text file common.php Class Common data class skeleton
Accessible without login Plain text file example.php Example Typical using samples
Plain text file gateway.php Class Gateway class
Accessible without login Plain text file LICENSE Data Auxiliary data
Accessible without login Plain text file README.md Data Auxiliary data

 Version Control Unique User Downloads Download Rankings  
 100%
Total:271
This week:0
All time:7,697
This week:116Up