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: 87 All time: 9,916 This week: 284Up
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: 33
All time rank: 6831 in Benin Benin
Week rank: 19 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());


?>


  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:87
This week:0
All time:9,916
This week:284Up