PHP Classes
elePHPant
Icontem

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

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Info   View files Example   View files View files (5)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Last Updated Ratings Unique User Downloads Download Rankings  
2015-07-13 (21 days ago) RSS 2.0 feedNot yet rated by the usersTotal: 235 This week: 1All time: 7,247 This week: 1,067Up
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
Name: Vallo Reima is available for providing paid consulting. Contact Vallo Reima .
Classes: 3 packages by
Country: Estonia Estonia
Age: 58
All time rank: 24129 in Estonia Estonia
Week rank: 571 Up3 in Estonia Estonia Up
Innovation award
Innovation award
Nominee: 1x

Details provided by the author  

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

    [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:235All time:7,247
 This week:1This week:1,067Up