PHP Classes

File: readme.txt

Recommend this page to a friend!
  Classes of zinsou A.A.E.Mo´se  >  PHP Resource Type Extension  >  readme.txt  >  Download  
File: readme.txt
Role: Documentation
Content type: text/plain
Description: readme
Class: PHP Resource Type Extension
Extend PHP resource values to save and load values
Author: By
Last change:
Date: 3 years ago
Size: 3,602 bytes
 

Contents

Class file image Download
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. 
For more information send a message to info at phpclasses dot org.