Jump to content

Shorthand object creation syntax ( obj->property )


davestewart

Recommended Posts

*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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

  • 3 years later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.