PHP Classes

PHP Public Wrap: Access an object private variables and functions

Recommend this page to a friend!
     
  Info   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 313 All time: 7,283 This week: 660Up
Version License PHP version Categories
public-wrap 1.0BSD License5.3PHP 5, Language, Testing
Description 

Author

This class can access an object private attributes and methods.

It takes a given object and uses reflection to access its private attributes and methods from outside.

The class implements __set, __get and __call methods to provide the access to the wrapped object externally.

Picture of Ninsuo
  Performance   Level  
Name: Ninsuo <contact>
Classes: 2 packages by
Country: France France
Age: 40
All time rank: 326386 in France France
Week rank: 180 Up12 in France France Up

Details

PublicWrap Set properties and methods visibility to public This class doesn't change behaviour of your original object : it only wraps property and method calls to make them visibles using reflection. This is useful when you're doing unit tests on protected and private methods, or if you want to mock a protected/private dependancy of an object. --- Quick usage sample : class A { private $x = 42; } Normal behaviour : $test = new A(); echo $test->x; // fatal error: $x is private With PublicWrap : $test = new A(); $proxy = new PublicWrap($test); echo $test->x; // 42 See demo for more details (works with private and protected methods and properties). --------- How does it work ? PHP has magic methods: __get($property) let us implement the access of a $property on an object __set($property, $value) let us implement the assignation of a $property on an object __call($method, $args) let us implement a $method call PHP has reflectiion classes: Reflection is a powerful tool that let us access internal constructs of classes in PHP. --------- Changelog 1.0 *** Original version ***

  Files folder image Files (4)  
File Role Description
Plain text file PublicWrap.php Class Core class
Accessible without login Plain text file PublicWrap.demo.php Example Demo: a full usage example
Accessible without login Plain text file readme.txt Doc. Documentation
Accessible without login Plain text file PublicWrap.test.php Test PHPUnit tests

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 0%
Total:313
This week:0
All time:7,283
This week:660Up