Viewed   57 times

Right now, I have this code where $obj_arr maybe contain array and an object.

$obj_temp = array ($obj_identity, $arr_user, $obj_locale, $arr_query); 

foreach ($obj_temp as $maybe_arr) {
        if (is_array($maybe_arr)) :
            $name = (string) key($maybe_arr);
        if (is_object($maybe_arr)) :    
            ???? // how to retrieve a class name ?
        endif;  

        $obj_arr[$name] = $maybe_arr;

}



obj_will_be_extract($obj_arr);

function obj_will_be_extract($obj_arr) {
    extract($obj_arr);

    //Do the rest

}

I need to create an array consist of combination of objects and arrays. Cause I need to extract it, then how to get an object name?

 Answers

2

Use get_class to get the class name of an object.

Saturday, December 3, 2022
2

You can do

function unsetValue(array $array, $value, $strict = TRUE)
{
    if(($key = array_search($value, $array, $strict)) !== FALSE) {
        unset($array[$key]);
    }
    return $array;
}

You can also use spl_object_hash to create a hash for the objects and use that as array key.

However, PHP also has a native Data Structure for Object collections with SplObjectStorage:

$a = new StdClass; $a->id = 1;
$b = new StdClass; $b->id = 2;
$c = new StdClass; $c->id = 3;

$storage = new SplObjectStorage;
$storage->attach($a);
$storage->attach($b);
$storage->attach($c);
echo $storage->count(); // 3

// trying to attach same object again
$storage->attach($c);
echo $storage->count(); // still 3

var_dump( $storage->contains($b) ); // TRUE
$storage->detach($b);
var_dump( $storage->contains($b) ); // FALSE

SplObjectStorage is Traversable, so you can foreach over it as well.

On a sidenote, PHP also has native interfaces for Subject and Observer.

Tuesday, August 30, 2022
 
ntr
 
ntr
5

In PHP 5.5 onwards you can do this using ::class:

echo MyClass::class;

Unfortunately this is not possible at all on earlier versions, where you have to either hardcode the class name or create an instance first and use get_class on it.

Saturday, October 15, 2022
 
kalium
 
5
function benchmark($func_name, $iterations) {
    $begin = microtime(true);
    for ($i = 0; $i < $iterations; $i++) {
        $func_name();
    }
    $end = microtime(true);

    $execution_time = $end - $begin;

    echo $func_name , ': ' , $execution_time;
}

function standClass() {
    $obj = new stdClass();
    $obj->param_one = 1;
    $obj->param_two = 2;
}

function castFromArray() {
    $obj = (object)array('param_one' => 1, 'param_two' => 2);
}


benchmark('standClass', 1000);
benchmark('castFromArray', 1000);

benchmark('standClass', 100000);
benchmark('castFromArray', 100000);

Outputs:

standClass: 0.0045979022979736
castFromArray: 0.0053138732910156

standClass: 0.27266097068787
castFromArray: 0.20209217071533

Casting from an array to stdClass on the fly is around 30% more efficient, but the difference is still negligible until you know you will be performing the operation 100,000 times (and even then, you're only looking at a tenth of a second, at least on my machine).

So, in short, it doesn't really matter the vast majority of the time, but if it does, define the array in a single command and then type-cast it to an object. I definitely wouldn't spend time worrying about it unless you've identified the code in question as a bottleneck (and even then, focus on reducing your number of iterations if possible).

Saturday, December 3, 2022
 
zroth
 
2

It's definitely not possible, other than some hackish way.

Why not just add a name parameter to init, like this:

def __init__(self, name):
    self.name = name

myObjectClass = myClass("myObjectClass")

This is how I'd actually do it..

Friday, August 12, 2022
 
anvaka
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :