PHP cast array for an object inside a class. Is it possible?

I have a type variable

$a = array(
       'first' => array( 'b' => 2, 'c' => 3),
       'second' => array('d' => 4, 'e' => 5)
);

To access an item, I can use

$a['first']['c']

But to access this,

$a->first->c

I can convert the array to an object as follows:

$a = (object)array(
       'first' => (object)array( 'b' => 2, 'c' => 3),
       'second' => (object)array('d' => 4, 'e' => 5)
);

But I have to use the same inside such a class.

class className {
     public static $a = (object)array(
           'first' => (object)array( 'b' => 2, 'c' => 3),
           'second' => (object)array('d' => 4, 'e' => 5)
    );
}

It throws a T_OBJECT_CAST error. How can I make it work if I want to access an element of type

className::$a->first->c;
+3
source share
4 answers

Constant expressions in PHP are pretty limited right now (although there are some changes in the works). You can initialize a static variable after defining the class or in a static initialization function that you will need to explicitly call.

class ClassName {
     public static $a;
}
ClassName::$a = (object)array(
       'first' => (object)array( 'b' => 2, 'c' => 3),
       'second' => (object)array('d' => 4, 'e' => 5),
    );

, . , .

class ClassName {
    public static $a;
    static function initialize() {
        static $notRun = TRUE;
        if ($notRun) {
            $notRun = FALSE;
            self::$a = (object)array(
              'first' => (object)array( 'b' => 2, 'c' => 3),
              'second' => (object)array('d' => 4, 'e' => 5),
            );
            # other tasks...
        }
    }
}
ClassName::initialize();
+3

, static member

:

LIVE DEMO

<?php

class sample
{

     public static $a;

     function __construct() {

          self::$a = (object)array(
                   'first' => (object)array( 'b' => 2, 'c' => 3),
                   'second' => (object)array('d' => 4, 'e' => 5)
                    );
     }

}

// $obj = new sample();
var_dump(sample::$a->first);
+4

. . PHP ( RFC - ) (.. , , ).

, :

public function __construct()
{
   //non-static property:
   $this->a = (object)array(
           'first' => (object)array( 'b' => 2, 'c' => 3),
           'second' => (object)array('d' => 4, 'e' => 5)
    );
}

- for normal properties. For static properties, of course, you can use self::- but this will not be related to instantiation (obviously, this is because it is static)

By the way, there is a trick with converting an array to an object:

$a = array(
       'first' => array( 'b' => 2, 'c' => 3),
       'second' => array('d' => 4, 'e' => 5)
);

//object:
$a = json_decode(json_encode($a));
+3
source
class Foo {
    public static $a;

    public function __construct() {
        $a = array(
            'first' => array( 'b' => 2, 'c' => 3 ),
            'second' => array( 'd' => 4, 'e' => 5 )
        );
        self::$a = json_decode( json_encode( $a ) );
    }
}

$foo = new Foo();

var_dump( $foo::$a->first->c );
+2
source

All Articles