Nathan Rixham | 1 Jun 03:04 2009
Picon

[PHP-DEV] need inverted __sleep?

Hi All,

hoping somebody can help me here..

I need to implement an inverted __sleep method, by which I mean to 
specify what variables should not be included.

use case:
<?php
class base
{
   private $notToBeSerialized;

   public function __sleep()
   {
     // TODO code return instance properties excluding $notToBeSerialized
   }

}

class foo extends base
{
   private $bar;
}

class poo extends foo
{
   private $baz;
}

(Continue reading)

Matt Wilson | 1 Jun 03:44 2009
Picon

Re: [PHP-DEV] need inverted __sleep?

get_class_vars + array_diff

On May 31, 2009, at 8:04 PM, Nathan Rixham wrote:

> Hi All,
>
> hoping somebody can help me here..
>
> I need to implement an inverted __sleep method, by which I mean to  
> specify what variables should not be included.
>
> use case:
> <?php
> class base
> {
>  private $notToBeSerialized;
>
>  public function __sleep()
>  {
>    // TODO code return instance properties excluding  
> $notToBeSerialized
>  }
>
> }
>
> class foo extends base
> {
>  private $bar;
> }
>
(Continue reading)

Nathan Rixham | 1 Jun 03:46 2009
Picon

Re: [PHP-DEV] need inverted __sleep?

Matt Wilson wrote:
> get_class_vars + array_diff

cheers but nope; as the manual says
"Returns an associative array of default public properties of the class"

need private and inherited private

> On May 31, 2009, at 8:04 PM, Nathan Rixham wrote:
> 
>> Hi All,
>>
>> hoping somebody can help me here..
>>
>> I need to implement an inverted __sleep method, by which I mean to 
>> specify what variables should not be included.
>>
>> use case:
>> <?php
>> class base
>> {
>>  private $notToBeSerialized;
>>
>>  public function __sleep()
>>  {
>>    // TODO code return instance properties excluding $notToBeSerialized
>>  }
>>
>> }
>>
(Continue reading)

Matt Wilson | 1 Jun 03:59 2009
Picon

Re: [PHP-DEV] need inverted __sleep?

mwilson <at> MattW-Mac:~$ php -r 'class a { public $a, $b, $c, $d; public  
function __sleep() { return array_diff( array_keys( (array) $this),  
array("b","c")); } } $a = new a; var_dump($a->__sleep());'
array(2) {
   [0]=>
   string(1) "a"
   [3]=>
   string(1) "d"
}

On May 31, 2009, at 8:46 PM, Nathan Rixham wrote:

> Matt Wilson wrote:
>> get_class_vars + array_diff
>
> cheers but nope; as the manual says
> "Returns an associative array of default public properties of the  
> class"
>
> need private and inherited private
>
>> On May 31, 2009, at 8:04 PM, Nathan Rixham wrote:
>>> Hi All,
>>>
>>> hoping somebody can help me here..
>>>
>>> I need to implement an inverted __sleep method, by which I mean to  
>>> specify what variables should not be included.
>>>
>>> use case:
(Continue reading)

Nathan Rixham | 1 Jun 04:01 2009
Picon

Re: [PHP-DEV] need inverted __sleep?

matt.. that's public properties on a single class - see the usecase

use case:
<?php
class base
{
   private $notToBeSerialized;

   public function __sleep()
   {
     // TODO code return instance properties excluding $notToBeSerialized
   }

}

class foo extends base
{
   private $bar;
}

class poo extends foo
{
   private $baz;
}

$p = new poo;
echo serialize($p);

Matt Wilson wrote:
> mwilson <at> MattW-Mac:~$ php -r 'class a { public $a, $b, $c, $d; public 
(Continue reading)

Jonathan Tapicer | 1 Jun 04:20 2009
Picon

Re: [PHP-DEV] need inverted __sleep?

Hi,

Matt's approach works also for your usecase, casting to array returns
all the properties of the class and base classes, but it has some
problems: for private properties the class name is added before the
property name, and for protected properties * is added, both things
added are between null characters, so you can remove the added string
with some regexp.

Another approach that will work is using ReflectionClass and the
method getProperties, it will return all the properties (even
private), but only of the class itself, it won't return the properties
declared in base classes, you have the get the base class
(get_parent_class) and recursively do the same thing for all the base
classes.

Regards,
Jonathan

On Sun, May 31, 2009 at 11:01 PM, Nathan Rixham <nrixham <at> gmail.com> wrote:
> matt.. that's public properties on a single class - see the usecase
>
> use case:
> <?php
> class base
> {
>  private $notToBeSerialized;
>
>  public function __sleep()
>  {
(Continue reading)

internals | 1 Jun 11:30 2009
Picon
Picon

[PHP-DEV] PHP 5 Bug Summary Report

 PHP 5 Bug Database summary - http://bugs.php.net/

 Num Status     Summary (1326 total -- which includes 867 feature requests)
===============================================[*Network Functions]===========
48167 To be documented  undefined function checkdnsrr()
===============================================[*XML functions]===============
48095 Verified   Load RDF Format Error
===============================================[Apache2 related]==============
32220 Assigned   [PATCH] thread_resources for thread not getting freed when apache kills thread
47675 Open       File descriptor leaked due to HAVE_BROKEN_GETCWD
47681 Open       System TMP dir ignored in file uploads
48134 Open       crash after a few days (backtrace attached) with worker MPM
48260 Open       Size of PHP file affects behaviour of virtual() or #include virtual
===============================================[Arrays related]===============
47221 Open       no result from array_diff()
48405 Feedback   Segmentation fault while type casting array into object
===============================================[BC math related]==============
44995 Open       bcpowmod() using a scale function always returns 0
46564 Verified   bcmod( '1071', '357.5' ) returns '0'
===============================================[Bzip2 Related]================
29521 Assigned   compress.bzip2 wrapper
===============================================[Calendar related]=============
40213 Suspended  easter_date() returns wrong timestamp if ...
===============================================[CGI related]==================
45217 Open       crash if -z and -m are used together
47042 Open       cgi sapi is incorrectly removing the SCRIPT_FILENAME for non apache
47412 Open       PHP_MSHUTDOWN_FUNCTION not being called under FastCGI
47605 Open       CGI SAPI can not send HTTP 200 header
47627 Open       "No input file specified" causing crash
47766 Feedback   php-cgi.exe crashes
(Continue reading)

internals | 1 Jun 12:00 2009
Picon
Picon

[PHP-DEV] PHP 6 Bug Summary Report

 PHP 6 Bug Database summary - http://bugs.php.net/

 Num Status     Summary (76 total -- which includes 36 feature requests)
===============================================[*Unicode Issues]==============
48265 Open       Source and result of database have different encodings.
===============================================[Apache related]===============
47061 Open       User not logged under Apache
===============================================[Apache2 related]==============
44083 Open       virtual() not outputting results if zlib.output_compression = On
===============================================[Arrays related]===============
35277 Suspended  incorrect recursion detection
41758 Assigned   SORT_LOCALE_STRING broken for sort() in PHP6
43109 Open       array_intersect() emits unexpected no of notices when 2d array is passed as arg
===============================================[COM related]==================
45836 Open       cannot use com 
46909 Open       COM object not allowing calls to methods
===============================================[Compile Failure]==============
42606 Open       unicode/constants.c relies on ICU draft api
44502 Suspended  Compiling ok with MySQL 5.0
===============================================[Date/time related]============
46948 Assigned   ext/date/lib/parse_tz.c:99: Memory leak: buffer
===============================================[Filesystem function related]==
42110 Open       fgetcsv doesn't handle ""\n correctly in multiline csv record
44034 Open       FILE_IGNORE_NEW_LINES in FILE does not work as expected when lines end in \r\n
46688 Open       Return values differ from 5.3 and are also inconsistent
46689 Open       Downcoded notices suggest unfinished code in file system?
===============================================[GD related]===================
34670 Assigned   imageTTFText for Indian scripts (Devanagari)
34992 Assigned   imageconvolution does not respect alpha
===============================================[I18N and L10N related]========
(Continue reading)

Nathan Rixham | 1 Jun 13:34 2009
Picon

Re: [PHP-DEV] need inverted __sleep?

Jonathan Tapicer wrote:
> Hi,
> 
> Matt's approach works also for your usecase, casting to array returns
> all the properties of the class and base classes, but it has some
> problems: for private properties the class name is added before the
> property name, and for protected properties * is added, both things
> added are between null characters, so you can remove the added string
> with some regexp.
> 
> Another approach that will work is using ReflectionClass and the
> method getProperties, it will return all the properties (even
> private), but only of the class itself, it won't return the properties
> declared in base classes, you have the get the base class
> (get_parent_class) and recursively do the same thing for all the base
> classes.
> 
> Regards,
> Jonathan

doh, thanks Jonathan and thanks Matt - completely missed that (array) 
even though I'd been using it in a different attempt using Serializable

small note is that you do not need to remove the class names or 
suchlike, this works fine (and I assume is faster than recursive reflection)

public function __sleep()
{
   $exclude = chr(0) . __CLASS__ . chr(0) . 'notToBeSerialized';
   return array_diff(array_keys((array)$this) , array($exclude) );
(Continue reading)

Kalle Sommer Nielsen | 1 Jun 16:13 2009
Picon
Picon

Re: [PHP-DEV] need inverted __sleep?

Hi

2009/6/1 Nathan Rixham <nrixham <at> gmail.com>:
> Matt Wilson wrote:
>>
>> get_class_vars + array_diff
>
> cheers but nope; as the manual says
> "Returns an associative array of default public properties of the class"

I've recently fixed the manual about get_class_vars, since PHP 5.0.5
get_class_vars, depending on the scope will return the properties
thats accessable from this scope, so calling it inside a method will
expose both its public/protected/private properties.

>
> need private and inherited private
>

-- 
regrads,

Kalle Sommer Nielsen
kalle <at> php.net

--

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

(Continue reading)


Gmane