3

I have a variable like

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

To access an element, I can use

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

But to access it like this,

$a->first->c

I can cast the array into 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 a class like this..

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 the element like

className::$a->first->c;

4 Answers 4

4

Note that static member variable shares the memory among all objects of same class

You can try this:

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);
Sign up to request clarification or add additional context in comments.

Comments

3

Constant expressions in PHP are rather limited right now (though there are some changes in the works). You can initialize the static variable after the class definition, or in a static initialization function, which you will need to call explicitly.

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

The advantage of using an initialization function is that you can encapsulate other class initialization tasks. If there aren't other tasks, it can be overkill.

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();

Comments

3

Currently, you can not use expressions in default class properties definitions. Only plain values can be used for that. In future PHP versions it will be allowed to use static expressions (this RFC is by Bob Weinand) in such definitions (i.e. expressions which can be evaluated instantly, not in run-time).

You'll have to use constructor to define your property value, like:

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 it will not be bound to instantiation (obviously, that's because it's static)

By the way, there's a trick with array to object conversion:

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

//object:
$a = json_decode(json_encode($a));

Comments

2
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 );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.