Safe_DataObject.php

Summary
Safe_DataObject.php
Safe_DataObjectExtension of DB_DataObject
Safe_DataObject
__destructDestructor to free global memory resources associated with this data object when it’s unset or goes out of scope.
__cloneMagic function called at clone() time.
__sleepMagic function called at serialize() time.
__wakeupMagic function called at unserialize() time.
__callMagic function called when someone attempts to call a method that doesn’t exist.
databaseStructureWork around memory-leak bugs...
parseIniFilesWhen switching site configurations, DB_DataObject was loading its .ini files over and over, leaking gobs of memory.

Safe_DataObject

Extension of DB_DataObject

postActiv

the micro-blogging software

Copyright

Copyright © 2016-2017, Maiyannah Bishop

Derived from code copyright various sources

  • GNU Social © 2013-2016, Free Software Foundation, Inc
  • StatusNet © 2008-2012, StatusNet, Inc

License

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this program.  If not, see http://www.gnu.org/licenses/.

https://www.gnu.org/licenses/agpl.html

About

Extended DB_DataObject to improve a few things:

  • free global resources from destructor
  • remove bogus global references from serialized objects
  • don’t leak memory when loading already-used .ini files (eg when using the same schema on thousands of databases)

PHP version

Tested with PHP 7

File Authors

Web

Safe_DataObject

Extended DB_DataObject to improve a few things

  • free global resources from destructor
  • remove bogus global references from serialized objects
  • don’t leak memory when loading already-used .ini files (eg when using the same schema on thousands of databases)
Summary
__destructDestructor to free global memory resources associated with this data object when it’s unset or goes out of scope.
__cloneMagic function called at clone() time.
__sleepMagic function called at serialize() time.
__wakeupMagic function called at unserialize() time.
__callMagic function called when someone attempts to call a method that doesn’t exist.
databaseStructureWork around memory-leak bugs...
parseIniFilesWhen switching site configurations, DB_DataObject was loading its .ini files over and over, leaking gobs of memory.

__destruct

function __destruct()

Destructor to free global memory resources associated with this data object when it’s unset or goes out of scope.  DB_DataObject doesn’t do this yet by itself.

__clone

function __clone()

Magic function called at clone() time.

We use this to drop connection with some global resources.  This supports the fairly common pattern where individual items being read in a loop via a single object are cloned for individual processing, then fall out of scope when the loop comes around again.

As that triggers the destructor, we want to make sure that the original object doesn’t have its database result killed.  It will still be freed properly when the original object gets destroyed.

__sleep

function __sleep()

Magic function called at serialize() time.

We use this to drop a couple process-specific references from DB_DataObject which can cause trouble in future processes.

Returns

  • array of variable names to include in serialization.

__wakeup

function __wakeup()

Magic function called at unserialize() time.

Clean out some process-specific variables which might be floating around from a previous process’s cached objects.

Old cached objects may still have them.

__call

function __call($method,
$params)

Magic function called when someone attempts to call a method that doesn’t exist.  DB_DataObject uses this to implement setters and getters for fields, but neglects to throw an error when you just misspell an actual method name.  This leads to silent failures which can cause all kinds of havoc.

Parameters

  • string $method
  • array $params

Returns

  • mixed

Error Statues

  • throws Exception

databaseStructure

function databaseStructure()

Work around memory-leak bugs...  Had to copy-paste the whole function in order to patch a couple lines of it.  Would be nice if this code was better factored.

@param optional string name of database to assign / read @param optional array structure of database, and keys @param optional array table links

Scope

  • public

Returns

  • true or PEAR:error on wrong paramenters.. or false if no file exists.. or the array(tablename => array(column_name=>type)) if called with 1 argument..  (databasename)

parseIniFiles

protected function parseIniFiles(array $schemas)

When switching site configurations, DB_DataObject was loading its .ini files over and over, leaking gobs of memory.  This refactored helper function uses a local cache of .ini files to minimize the leaks.

Parameters

  • array of .ini file names $schemas

Returns

  • array
function __destruct()
Destructor to free global memory resources associated with this data object when it’s unset or goes out of scope.
function __clone()
Magic function called at clone() time.
function __sleep()
Magic function called at serialize() time.
function __wakeup()
Magic function called at unserialize() time.
function __call($method,
$params)
Magic function called when someone attempts to call a method that doesn’t exist.
function databaseStructure()
Work around memory-leak bugs...
protected function parseIniFiles(array $schemas)
When switching site configurations, DB_DataObject was loading its .ini files over and over, leaking gobs of memory.