PHP Classes

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    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 88 This week: 1All time: 9,958 This week: 560Up
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
Picture of zinsou A.A.E.Moïse
  Performance   Level  
Name: zinsou A.A.E.Moïse is available for providing paid consulting. Contact zinsou A.A.E.Moïse .
Classes: 50 packages by
Country: Benin Benin
Age: 34
All time rank: 6781 in Benin Benin
Week rank: 109 Up1 in Benin Benin Equal
Innovation award
Innovation award
Nominee: 23x

Winner: 2x

Example

<?php
require_once('Extended_resource.class.php');

echo
'<pre>';
$x=new resource('fopen("'.str_replace("\\","/",__FILE__).'","rb+")');
$x->dump();

echo
'<br>';
echo
'<br>';

$x->export();
echo
'<br>';
echo
'<br>';

print_r($x);
echo
'<br>';
echo
'<br>';

var_dump(
$x->type(),
ftell($x->use_resource())
);


fseek($x->use_resource(),98);

$u=$x->use_resource();
echo
'<br>';
echo
'<br>';
var_dump(ftell($u));
echo
'<br>';
echo
'<br>';

var_dump($y=serialize($x));
echo
'<br>';
echo
'<br>';

var_dump(unserialize($y));
echo
'<br>';
echo
'<br>';

try{
   
resource();
}catch (
Exception $e) {
    echo
'Exception : ', $e->getMessage(), "\n";
}
echo
'<br>';
echo
'<br>';

$u=var_export(unserialize($y),true);
echo
'<br>';
echo
'<br>';

eval(
'$t = '.$u.';');

var_dump($t);


var_dump(clone($t));

class
test{
    public
$property,$position=null;
   
    public function
__construct(){
       
$this->property=resource('fopen("'.str_replace("\\","/",__FILE__).'","rb+")');
       
    }
   
    public function
seek($position,$whence=false){
       
fseek($this->property->use_resource(),$position,$whence);
       
$this->position=$position;
    }
   
   
    public function
gets($length=null){
        if(
is_int($length)) $line=fgets($this->property->use_resource(),$length);
        else
$line=fgets($this->property->use_resource());
       
$this->position=ftell($this->property->use_resource());
        return
$line;
           
    }
   
    public function
tell(){
        return
$this->position;
    }
                       
    public function
__wakeup(){
       
$this->seek($this->position);
    }
                   
                   
}

$test= new test();

var_dump($test);
$content='';
while(
$line=$test->gets()){
   
$content.=$line;
   
var_dump($test->tell());
}

highlight_string($content);

$test2=serialize($test);
echo
'<br>';
echo
'<br>';

print_r($test2=unserialize($test2));
echo
'<br>';
echo
'<br>';
var_dump($test2->tell());


?>


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:88
This week:1
All time:9,958
This week:560Up