473,748 Members | 2,602 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1840
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*******@attgl obal.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 getNestedProper ty like so:

<?

function getNestedProper ty($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 = getNestedProper ty(new c,
array('arr', 'a', 1)));

?>

Regards,

John Peters

On May 28, 8:51 am, Hugh Oxford <ares...@fas.co mwrote:
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 getNestedProper ty like so:

<?

function getNestedProper ty($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 = getNestedProper ty(new c,
array('arr', 'a', 1)));

?>

Regards,

John Peters

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

I can reference is manually thus:

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

but if I build a string...

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

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

print_r($thi s->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
2438
by: Phil Powell | last post by:
Consider this: class ActionHandler { /*------------------------------------------------------------------------------------------------------------------------------------------------------------------- This class will be a singleton class structure by calling its constructor by reference only (prefixed by '&'). Is primarly used to reference its errorArray Array property as a single reference to allow for static usage
10
2345
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 similar name to the table. Rather than making a long Select Case that could become obsolete if lookup tables are added and the source table of the ListBox is edited I came up with this code.) This code works but of course it gives me build...
4
1433
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 subobjects of an object of derived type. I thought I had seen a way to use references to statically access member functions in a way syntactically similar to how pointers dynamically access the same objects member functions. The overall objective...
0
2333
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 follows: - 'CheckBox' is an ambiguous reference 'CheckBox' is an ambiguous reference 'Label' is an ambiguous reference etc................. Could someone please tell me where I have gone wrong. Here is the rpx.cs file if this helps.
4
1838
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 references the dll where the type of the parameter is defined. I am able to create to create my class in a form without any build errors and without any references to the DLL where the other parameter type is defined. The form only uses the...
4
5051
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, websites, MSDN and was not able to find something that would help me understand and code. I might not be searching for the right words or phrases. So if you know how to do this or know of links or websites that have information about this, please...
1
1217
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> #include <string>
1
2731
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 I have only two non-trivial methods: In OnConnection I add my event handler to OnBuildProjConfigDone, and in the event handler I executed Build.Cancel. In IDE - no problem. When I execute I see that AddIn works and Build.Cancel is called but...
3
4681
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 "ResolveAssemblyReference" task failed unexpectedly. System.BadImageFormatException: Could not load file or assembly 'C:\System.exe' or one of its dependencies. The module was expected to
0
8823
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9530
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9312
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9238
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8237
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.