note 114046 added to class.reflectionproperty

From: Date: Mon, 06 Jan 2014 12:36:52 +0000
Subject: note 114046 added to class.reflectionproperty
Groups: php.notes 
Request: Send a blank email to php-notes+get-198127@lists.php.net to get a copy of this message
<?php //serialize static properties (class variable) class Student { private $members = array(); protected $name; public static $noOfStudent; public function __construct($name = 'Nanhe Kumar') { $this->name = $name; Student::$noOfStudent++; } public function __sleep() { $vars = get_class_vars(get_class($this)); foreach ($vars as $key => $val) { if (!empty($val)) $this->members[$key] = $val; } return array_keys(get_object_vars($this)); } public function __wakeup() { foreach ($this->members as $key => $val) { $prop = new ReflectionProperty(get_class($this), $key); $prop->setValue(get_class($this), $val); } $this->members = array(); } public function getTotalStudent() { return self::$noOfStudent; } } $so1 = new Student(); $so2 = new Student(); $serialized = serialize($so1); print_r($serialized); //O:7:"Student":2:{s:16:"Studentmembers";a:1:{s:11:"noOfStudent";i:2;}s:7:"*name";s:11:"Nanhe Kumar";} $unserialized = unserialize($serialized); print_r($unserialized); //Student Object ( [members:Student:private] => Array ( ) [name:protected] => Nanhe Kumar ) echo Student::$noOfStudent; //2 ?> ---- Server IP: 72.52.91.14 Probable Submitter: 72.52.91.14 ---- Manual Page -- http://php.net/manual/en/class.reflectionproperty.php Edit -- https://master.php.net/note/edit/114046 Del: integrated -- https://master.php.net/note/delete/114046/integrated Del: useless -- https://master.php.net/note/delete/114046/useless Del: bad code -- https://master.php.net/note/delete/114046/bad+code Del: spam -- https://master.php.net/note/delete/114046/spam Del: non-english -- https://master.php.net/note/delete/114046/non-english Del: in docs -- https://master.php.net/note/delete/114046/in+docs Del: other reasons-- https://master.php.net/note/delete/114046 Reject -- https://master.php.net/note/reject/114046 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#198127) next »