PHP Classes
elePHPant
Icontem

PHP Resource Type Extension: Extend PHP resource values to save and load values

Recommend this page to a friend!
  Info   View files Example   View files View files (5)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2018-05-15 (4 months ago) RSS 2.0 feedNot yet rated by the usersTotal: 77 All time: 9,168 This week: 393Up
Version License PHP version Categories
e-resource 1.0.0Custom (specified...5PHP 5, Data types
Description Author

This class can extend PHP resource values to save and load values.

An object of this class take a resource values returned for instance from fopen or imagepng functions and provides functions to save the object values to a string using the regular PHP serialize or export functions.

Other PHP functions like unserialize can be use restore the object to its original state from a string returned from the serialized function.

Innovation Award
PHP Programming Innovation award nominee
May 2018
Number 7
When PHP for instance opens a file or a database connection, it creates a resource number value that is used by PHP internally to determine what is the resource that was created.

PHP resources are just numbers, but PHP does not provide a means to save those resource numbers and be able to recreate the original resource and reload it later.

This package provides a solution that overcomes that PHP limitation. It can work with regular PHP functions like serialize, unserialize and var_export.

Manuel Lemos
  Performance   Level  
Name: zinsou A.A.E.Mose is available for providing paid consulting. Contact zinsou A.A.E.Mose .
Classes: 45 packages by
Country: Benin Benin
Age: 28
All time rank: 10341 in Benin Benin
Week rank: 22 Up1 in Benin Benin Equal
Innovation award
Innovation award
Nominee: 17x

Winner: 2x

Details
Resources are specific type of PHP which can not been exported or serialized.Of course most of times nobody need to export or serialize a resource.However if the magical methods __sleep
and __wakeup exist for classes in PHP,it is eventually for cleaning and trying  to restore things like resources and other that would destroyed. the limitations  on resource can be bypassed .
The PHP Extended Resource Package helps to achieve this by providing a resource object which replace the real resource in the script.This resource object can then be serialized,
unserialized,exported and so one...This way, using resources in different objects is less painful.


How to use:

first be sure to include the main class source then you can create a new resource object with either  $resource=resource('your php code to create any type resource here');
or  $resource= new resource('your php code to create any type resource here'); then you can use this object.

eg: $resource=resource('fopen("'.str_replace("\\","/",__FILE__).'","rb+")');

$resource->export() //will print  'fopen("the/path/tothe/file.php","rb+")'

$resource->export(true) //will return  'fopen("the/path/tothe/file.php","rb+")'


to use the resource itself you just need to use $resource->use_resource();  or $resource->r();

eg:

echo ftell($resource->use_resource()); //will print 0;

fseek($resource->r(),100);

echo ftell($resource->use_resource()); //will print 100;


The object resource revealed itself particularly useful when you want to use a resource in an object as the value of a propriety.Indeed the object resource can be serialized and 
<<unserialized>>  a completely transparent way as it supports __wakeup and __sleep methods. So use it in an object will be transparently handled as the object can be serialized,the resource
object will be too and the inverse is also possible.Note that with a resource Object the PHP code of the current resource can be exported with the method export
but the resource object can also be exported and imported respectively via the PHP var_export function and the PHP magical object method __set_state();

One of the most helpful feature is the recover method :

Indeed one way to free space allocated to a resource is to use specific function to destroy the resource eg: imagedestroy() ,fclose and so one...Of course  using one of those functions on
a resource Object eg: imagedestroy($resource->r())  will destroy the resource in the Resource Object but not the object itself.With the recover method you can until the destruction of the Resource Object 
restore the destroyed resource any time and anywhere. 




NB:
Actually the word  <<resource>> is, started from PHP 7, in the list of the soft reserved words but can be used in the functions and classes names  so I use it as
the resource object class name and as the shortcut function name too. But in the future This may be changed into eResource for extended resource.

The resource object constructor will throw an exception if an inappropriate string is given as parameter so you can
use a try catch bloc to handle this exception.


try{
	$resource= resource('imagecreate(750,856)');
}catch(Exception $e){
	 echo 'Exception  : ',  $e->getMessage(), "\n";
}

or

try{
	$resource= new resource('imagecreate(750,856)');
}catch(Exception $e){
	 echo 'Exception  : ',  $e->getMessage(), "\n";
}

I don't think someone use a resource of which creation is based on only user input,but if you do so you must validate properly the input before use it with the resource
class. 
  Files folder image Files  
File Role Description
Accessible without login Plain text file example.php Example example script
Accessible without login Plain text file example1.php Example example script
Plain text file Extended_resource.class.php Class class source
Accessible without login Plain text file license.txt Lic. license file
Accessible without login Plain text file readme.txt Doc. readme

 Version Control Unique User Downloads Download Rankings  
 0%
Total:77
This week:0
All time:9,168
This week:393Up