davestewart Posted December 11, 2007 Share Posted December 11, 2007 *Mods* PLEASE don't move this to the OOP section again. I've had no luck there, and it's not specifically an OOP question. Hi everyone, I just want to know if there's a shorthand way to create objects, as there is for arrays: // shorthand array creation $foo = array( 'bar' => 0, 'baz' => array ( 'name' => 'Dave', 'gender' => 'male' ) ) // is there a way to do this with objects? // not that I know so far... $foo = object( bar->0, 'baz' -> object ( name -> 'Dave', gender -> 'male' ) ) I find object-access syntax is often quicker and easier to write than array-access syntax. Have I missed something obvious that I can't just write it out this way? Cheers, Dave Quote Link to comment Share on other sites More sharing options...
BenInBlack Posted December 11, 2007 Share Posted December 11, 2007 can't you just have your Object return as an Array to 'baz'? Quote Link to comment Share on other sites More sharing options...
davestewart Posted December 11, 2007 Author Share Posted December 11, 2007 Not sure if you're getting it. The thing is, I just want to create an object the same way I'd create an array, then be able to access it as and when I like: obj->property->subproperty as you would with an array: arr['key']['subkey'] But I don't know how to, or if you can, just build an array like this in the IDE. Quote Link to comment Share on other sites More sharing options...
davestewart Posted December 11, 2007 Author Share Posted December 11, 2007 OK - I'm getting there: $obj->name = 'Dave'; $obj->skills->php = 'good'; print_r($obj); stdClass Object ( [name] => Dave [skills] => stdClass Object ( [code=php:0] => good ) ) So I got it. However I think I've just realised that I can't just write variables out this way for class variable declarations as I can with arrays. Unless I'm wrong? Cheers, Dave Quote Link to comment Share on other sites More sharing options...
davestewart Posted December 11, 2007 Author Share Posted December 11, 2007 A comparison of array and object declaration here, with array being MUCH easier to lay out and read with complicated hierarchies. First, object syntax: $obj->name = 'Dave'; $obj->skills->php = 'good'; print_r($obj); stdClass Object ( [name] => Dave [skills] => stdClass Object ( [code=php:0] => good ) ) Now, array syntax. $arr = array ( 'name' => 'Dave', 'skills' => array ( 'php' => 'good' ) ); print_r($arr); Array ( [name] => Dave [skills] => Array ( [code=php:0] => good ) ) What I want to be able to do is to write out objects in the same was as I would do with arrays. Is this possible? Quote Link to comment Share on other sites More sharing options...
trq Posted December 11, 2007 Share Posted December 11, 2007 What I want to be able to do is to write out objects in the same was as I would do with arrays. Is this possible? Unlkess im not getting you, you just did. Whats the problem? Quote Link to comment Share on other sites More sharing options...
davestewart Posted December 11, 2007 Author Share Posted December 11, 2007 Ah, a "Super Moderator Genius". Brilliant. As you saw, it's an OK way of writing stuff out, but not as flexible as the array syntax for hierarchies, but the main point is that it errors-out completely when it comes to class-variable declarations. (which is the underlying motivation of this whole thread) With an associative array I can group a bunch of related properties (and sub properties) within the class body, no problem: var $font = array ( 'size' => 9, 'family' => 'Meta Plus Book', 'weight' => 'bold', bounds => array ( 'top' => 0, 'right' => 100, 'bottom' => 20, 'left' => 0, ) ) However, as hard as I've tried, I've found no way to do this with objects, short of creating sub classes, which I COULD do, but if I was to so that (for example a FontMetrics class) I wouldn't be able to instantiate anything outside of a class method anyway, which makes things like default properties a pain in the bum. Also, I don't want the overhead or complexity - I just want a flexible way to hold onto related variables. So back to the original question - is there a shorthand way to build flexible Object hierarchies in PHP as there is with Arrays?. Does that make sense? Quote Link to comment Share on other sites More sharing options...
davestewart Posted December 11, 2007 Author Share Posted December 11, 2007 ignore this (double post) Quote Link to comment Share on other sites More sharing options...
davestewart Posted February 23, 2011 Author Share Posted February 23, 2011 Check it out - a really neat answer appeared on Stack Overflow: http://stackoverflow.com/questions/455800/does-php-feature-short-hand-syntax-for-objects Nice. Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted February 23, 2011 Share Posted February 23, 2011 $foo = (object)array( 'bar' => 0, 'baz' => (object)array ( 'name' => 'Dave', 'gender' => 'male' ) ); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.