PHP Doku:: Dynamic class and object aggregation of methods and properties - function.aggregate.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzVariablen- und typbezogene ErweiterungenObject Aggregation/Composition [PHP 4]Object Aggregation Funktionenaggregate

Ein Service von Reinhard Neidl - Webprogrammierung.

Object Aggregation Funktionen

<<aggregate_properties

aggregation_info>>

aggregate

(PHP 4 >= 4.2.0)

aggregate Dynamic class and object aggregation of methods and properties

Beschreibung

void aggregate ( object $object , string $class_name )

Aggregates methods and properties defined in a class to an existing object. Methods and properties with names starting with an underscore character (_) are considered private to the aggregated class and are not used, constructors are also excluded from the aggregation procedure.

Parameter-Liste

object

class_name

Rückgabewerte

Es wird kein Wert zurückgegeben.

Siehe auch


5 BenutzerBeiträge:
- Beiträge aktualisieren...
mail at bealers dot com
20.11.2008 19:14
I had to fix someone else's php4 code to work under php5 that was b0rked due to aggregate not working, here's how I did it.

<?php
 
class shell { }

$classes = array("foo", "bar", "baz");

function
php5aggregate($holdingClass, $className)
{
 
// use php5 reflection to suss out the method names for the given class
  // http://uk.php.net/manual/en/language.oop5.reflection.php
 
$reflect = new ReflectionClass($className);
  foreach (
$reflect->getMethods() as $method)
  {
   
// copy the method to $holdingClass
    // http://www.php.net/manual/en/function.runkit-method-copy.php
   
runkit_method_copy($holdingClass, $method->getName(), $className);
  }
}

if (
version_compare(PHP_VERSION, '5.0.0') === 1)
{
 
/**
  * aggregate() does not exist in php5, so use our custom function
  * note, our custom aggregate function takes the holding class *name*
  * which is instantiated afterwards, this is different to how the php4 function works
  */
 
foreach($classes as $value)
  {
   
php5aggregate("shell",$value);
  }

 
$shell = new shell;
}
else
{
 
// php4
 
$shell = new shell;
  foreach(
$classes as $value)
  {
   
aggregate($shell,$value);
  }
}
  
?>
kencomer at NOSPAM dot kencomer dot com
12.09.2005 6:22
For PHP5 applications, the aggregate functionality available through classkit has been incorporated into and replaced by runkit. Per the classkit page of the PHP manual:

"Note:  This extension has been replaced by runkit, which is not limited to class manipulation but has function manipulation, as well."

  http://php.net/manual/en/ref.runkit.php

Per the runkit page:

" This package is meant as a feature added replacement for the classkit package. When compiled with the --enable-runkit=classkit  option to ./configure, it will export classkit compatible function definitions and constants."
Matt Barry
28.03.2005 22:02
A note for those who may be implementing projects in PHP4 using aggregate(); these functions do not exist in PHP5.  For similar functionality, you can try using the Classkit extension:

http://us2.php.net/manual/en/ref.classkit.php
gmail pfayolle
7.01.2005 21:16
Note that even if this can be used to emulate multiple inheritance to some extent, an object in PHP can only be subclass of the class used in the class declaration and no other.

<?php

class A {}
class
B {}

class
C extends A
{
  function
C()
  {
   
aggregate($this, 'B'); // emulate multiple inheritance
 
}
}

$c = new C;
echo (int)
is_subclass_of($c, 'B');

/*
Output: 0
*/

?>
hewei at ied dot org dot cn
2.03.2003 12:34
YES you can use aggregation functions to simulate multiple inheritence.

function foo()
{
    aggregate($this, "bar");
    bar::bar();
}



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)