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;
Note that static member variable shares the memory among all objects of same class
You can try this:
<?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);
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 (ie 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));
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();
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 );
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.