...

View Full Version : Number of objects created?



phantom007
02-20-2010, 01:15 PM
Hi

I was wondering if there is anyway in php to know how many objects of a class has been created?


Thanks






class myclass {

function __construct(){

}


function myfunc(){


}



}

$obj = new myclass();




Thanks

abduraooft
02-20-2010, 01:19 PM
You'd need to have a static member variable and increment the value from the constructor.

phantom007
02-20-2010, 01:28 PM
An example would be appreciated :)

Dormilich
02-20-2010, 01:56 PM
class MyClass
{
static private $selfCount = 0;

public function __construct()
{
self::$selfCount++;
}
}

SKDevelopment
02-20-2010, 02:03 PM
Also you would need to decrement the counter in the class destructor if you need to know how many instances of the object exists currently:


<?php
class myclass
{
public static $counter=0;

public function __construct()
{
++self::$counter;
}

public function __destruct()
{
--self::$counter;
}
}

$o1 = new myclass();
$o2 = new myclass();
echo myclass::$counter . '<br />'; // outputs 2
unset($o1);
echo myclass::$counter . '<br />'; // outputs 1
?>

Though if you only need to know how many objects have been created no matter if they still exist or not, then decrementing should not be done of course ...

kbluhm
02-20-2010, 05:55 PM
This is an interesting request. There are still some question to be asked.

Example 1:


// Unique variable names
// This would be 2 counted instances
$user1 = new User;
$user2 = new User;


Example 2:


// Same variable names
// Would this be 2 counted instances, or 1?
$user = new User;
$user = new User;


Here is an alternative solution that will count variables using the instanceof comparator. This is based upon Example 2 above returning a count of 1.

A few gotchas:
- It avoids array keys named GLOBALS, to avoid infinite recursion on the base scope of $GLOBALS or get_defined_vars(). This is where previous suggestions using static properties would be handier. So you'd want to avoid assigning something like $_SESSION['GLOBALS'] = new ClassName; ... which is unlikely, but entirely possible.
- It only checks current variable values, and not the number of called constructors.
- It will not record classes called statically (ie: User::emailExists()) unless the static method returns a class instance.

A couple non-gotchas:
- Classes do not require modifications to the constructor or destructor to increment/decrement counts.
- Classes do not require a constructor or destructor.

It also relies on scope definition.


function count_instances( $class_name, Array $scope = array() )
{

// Initiate our count
$count = 0;

// If $class is an instance, grab the class name
if ( is_object( $class_name ) )
{
$class_name = get_class( $class_name );
}

// Iterate the current scope
foreach ( $scope as $key => $value )
{

switch ( TRUE )
{
// Avoid infinite recursion
case ( 'GLOBALS' == $key ):
break;
// Recurse into sub-arrays
case is_array( $value ):
$count += count_instances( $class_name, $value );
break;
// Count instances
case ( $value instanceof $class_name ):
$count++;
break;
}

}

return $count;

}


Example 1:


class User {};

$user1 = new User;
$user2 = new User;

echo count_instances( 'User', $GLOBALS ); // 2

// 'User' can also be any instance of the class
echo count_instances( new User, $GLOBALS ); // 2
echo count_instances( $user1, $GLOBALS ); // 2

// $GLOBALS could also be get_defined_vars()... we just need a scope is all
echo count_instances( new User, get_defined_vars() ); // 2


Example 2:


class User {};

$user = new User;
$user = new User;

echo count_instances( 'User', $GLOBALS ); // 1


Example 3:


class User {};
class Whatever {};

$user = new User;
$user = new Whatever;

echo count_instances( 'User', $GLOBALS ); // 0


Example 4:


class User {};
class Whatever extends User {};

$user = new User;
$user = new Whatever;

echo count_instances( 'User', $GLOBALS ); // 1


Example 5:


session_start();

class User {};

$user1 = new User;
$user2 = new User;

$_SESSION['user'] = new User;
$_POST['user'] = new User;

echo count_instances( 'User', $GLOBALS ); // 4


Example 6, using $_SESSION as the scope:


session_start();

class User {};

$user1 = new User;
$user2 = new User;

$_SESSION['user'] = new User;
$_POST['user'] = new User;

echo count_instances( 'User', $_SESSION ); // 1

Dormilich
02-20-2010, 06:00 PM
Example 2:


// Same variable names
// Would this be 2 counted instances, or 1?
$user = new User;
$user = new User;


obviously 2 (w/o destructor), since the constructor was executed twice. if you use the destructor decrementing, then 1.

kbluhm
02-20-2010, 06:20 PM
obviously 2 (w/o destructor), since the constructor was executed twice. if you use the destructor decrementing, then 1.

That question was obviously directed at the OP's ultimate desired functionality. What you're referencing is other suggestions.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum