473,387 Members | 1,903 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Build string to reference object

I want to build a string to reference an object.

I can reference is manually thus:

print_r($this->struct->parts[0]->parts[1]);

but if I build a string...

$string = "->parts[0]->parts[1]"

....and try to reference it thus...

print_r($this->struct{$string});
I get Fatal error : Cannot use object of type stdClass as array

Any thoughts?
Jun 2 '08 #1
3 1820
Hugh Oxford wrote:
I want to build a string to reference an object.

I can reference is manually thus:

print_r($this->struct->parts[0]->parts[1]);

but if I build a string...

$string = "->parts[0]->parts[1]"

...and try to reference it thus...

print_r($this->struct{$string});
I get Fatal error : Cannot use object of type stdClass as array

Any thoughts?
True. You can't do it that way. You could use eval() - but that's a
bad thing to use.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jun 2 '08 #2
I would say, typically this isn't necessary, as you can pass around a
reference to the nested objects that you want to update or you map
strings to objects.

If you have a large app which requires this type of dynamic path-based
access, you might consider using an XML document data structure and
querying it with XPath queries.

In general, your example could be directly implemented using either
eval:

$str = 'parts[0]->parts[1]';
eval("\$val = \$this->struct->$str;");
print_r($val);

Or with a function getNestedProperty like so:

<?

function getNestedProperty($obj, $path)
{
{
$val = $obj;
foreach ($path as $part) {
$val = is_object($val) ? $val->$part :
$val[$part];
}
return $val;
}

class c
{
function c()
{
$this->arr = array('a' =array(1, 2, 3));
}
}

print_r($val = getNestedProperty(new c,
array('arr', 'a', 1)));

?>

Regards,

John Peters

On May 28, 8:51 am, Hugh Oxford <ares...@fas.comwrote:
I want to build a string to reference an object.

I can reference is manually thus:

print_r($this->struct->parts[0]->parts[1]);

but if I build a string...

$string = "->parts[0]->parts[1]"

...and try to reference it thus...

print_r($this->struct{$string});

I get Fatal error : Cannot use object of type stdClass as array

Any thoughts?
Jun 2 '08 #3
petersprc wrote:
I would say, typically this isn't necessary, as you can pass around a
reference to the nested objects that you want to update or you map
strings to objects.

If you have a large app which requires this type of dynamic path-based
access, you might consider using an XML document data structure and
querying it with XPath queries.

In general, your example could be directly implemented using either
eval:

$str = 'parts[0]->parts[1]';
eval("\$val = \$this->struct->$str;");
print_r($val);

Or with a function getNestedProperty like so:

<?

function getNestedProperty($obj, $path)
{
{
$val = $obj;
foreach ($path as $part) {
$val = is_object($val) ? $val->$part :
$val[$part];
}
return $val;
}

class c
{
function c()
{
$this->arr = array('a' =array(1, 2, 3));
}
}

print_r($val = getNestedProperty(new c,
array('arr', 'a', 1)));

?>

Regards,

John Peters

On May 28, 8:51 am, Hugh Oxford <ares...@fas.comwrote:
>I want to build a string to reference an object.

I can reference is manually thus:

print_r($this->struct->parts[0]->parts[1]);

but if I build a string...

$string = "->parts[0]->parts[1]"

...and try to reference it thus...

print_r($this->struct{$string});

I get Fatal error : Cannot use object of type stdClass as array

Any thoughts?
Thanks Peter and for your other replies elsewhere.
Jun 2 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Phil Powell | last post by:
Consider this: class ActionHandler { ...
10
by: Douglas Buchanan | last post by:
I am using the following code instead of a very lengthly select case statement. (I have a lot of lookup tables in a settings form that are selected from a ListBox. The data adapters are given a...
4
by: Steven T. Hatton | last post by:
Below is some code I wrote to get a better understanding of the dynamic verses static type resolution. My intention was to see if there was a way to use references for the static access to the...
0
by: Stephen Cairns | last post by:
I have the following rpx file in a .Net solution and I am getting the following build errors which are driving me crazy and ive no idea where I have went wrong. The build errors I'm getting are as...
4
by: Vish | last post by:
Hi all, I am having a build error in one of the overloaded functions in my class. The function takes either a string as a parameter or a type referenced in another dll as a parameter. My class...
4
by: sydney.luu | last post by:
Hello, I would greatly appreciate if someone can show me how to dynamically build a Repeater with unknown number of columns at design time. I have looked various threads in this newsgroup,...
1
by: Allen Maki | last post by:
//These codes will build but wil not compile. I hope someone would know why; and tell me. //I got the below error message during compile time #include "stdafx.h" #include <string.h> ...
1
by: AlexZh | last post by:
Hi, I'd like to stop command line build by one project build failed. To do that I've created simple AddIn (see code below), that works fine for IDE and does not work for command line. In the AddIn...
3
by: NickP | last post by:
Hi there, Today I try to compile an application of mine and I am getting a whole list of bizaar errors.... ------------------------------------------------ Error 1 The...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.