473,387 Members | 1,529 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.

Adding fields to an object in PHP 4

Hello,

I have an object that looks like this:

shelterrental Object
(
[connectionError] =>
[changedVals] => Array ( )
[dbh] => Resource id #2
[id] => 1 [shelter] => 1
[renter] => Kevin Shakakhnopolis
[contact] => 504-899-4028
[date] => 2005-04-11
)

I would like to add a field or two under certain circumstances. I have
tried

$obj->newfield = 'xxx';
$obj->{'newfield'} = 'yyy';
$obj['newfield'] = 'zzz';

None of these methods work.

What is the correct syntax to add a field to an object in PHP 4?

TIA!

jg
Jul 17 '05 #1
5 3432
jerrygarciuh wrote:
I would like to add a field or two under certain circumstances. I
have tried

$obj->newfield = 'xxx';
$obj->{'newfield'} = 'yyy';


Both of these should work fine, do you get any error messages?

It would also be useful to see some of the code that generates the initial
object.
JW

Jul 17 '05 #2
"Janwillem Borleffs" <jw@jwscripts.com> wrote in message
news:42**********************@news.euronet.nl...
jerrygarciuh wrote:
I would like to add a field or two under certain circumstances. I
have tried

$obj->newfield = 'xxx';
$obj->{'newfield'} = 'yyy';


Both of these should work fine, do you get any error messages?

It would also be useful to see some of the code that generates the initial
object.
JW

JW,

Thank you for your rapid reply! Here is the function that builds the
objects from mySQL records. I am thinking my problem is actually caused by
using foreach on the array of objects and in PHP 4 I can't pass reference
like foreach ($arr as &$v).

Any advice most welcome!

jg

function &buildObj($result, $classname) {
if ($result) {
while($row = mysql_fetch_assoc($result)) {
if ($row === null) return null;

/* Create the object */
$obj =& new $classname;

/* Explode the array and set the objects's instance data
*/
foreach($row as $key => $value)
{
$obj->{$key} = $value;
}
$objs[] = $obj;
}
return $objs;
} // end if ($result)
}
Jul 17 '05 #3

"jerrygarciuh" <de*****@no.spam.nolaflash.com> wrote in message
news:IDg6e.965$H53.255@lakeread05...
"Janwillem Borleffs" <jw@jwscripts.com> wrote in message
news:42**********************@news.euronet.nl...
jerrygarciuh wrote:
I would like to add a field or two under certain circumstances. I
have tried

$obj->newfield = 'xxx';
$obj->{'newfield'} = 'yyy';


Both of these should work fine, do you get any error messages?

It would also be useful to see some of the code that generates the
initial object.
JW

JW,

Thank you for your rapid reply! Here is the function that builds the
objects from mySQL records. I am thinking my problem is actually caused
by using foreach on the array of objects and in PHP 4 I can't pass
reference like foreach ($arr as &$v).

Any advice most welcome!

jg

function &buildObj($result, $classname) {
if ($result) {
while($row = mysql_fetch_assoc($result)) {
if ($row === null) return null;

/* Create the object */
$obj =& new $classname;

/* Explode the array and set the objects's instance data
*/
foreach($row as $key => $value)
{
$obj->{$key} = $value;
}
$objs[] = $obj;
}
return $objs;
} // end if ($result)
}


My tests confirm that my problem is that foreach operates on a copy and i
need to pass by reference or use while().
Jul 17 '05 #4
jerrygarciuh wrote:
Thank you for your rapid reply! Here is the function that builds the
objects from mySQL records. I am thinking my problem is actually
caused by using foreach on the array of objects and in PHP 4 I can't
pass reference like foreach ($arr as &$v).


Passing arguments by reference to foreach would only be a benefit if you
want to modify them.

The problem is probably that you are doing something like:

$obj = buildObj($result, "stdClass");
$obj->foo = 'bar';

This obviously doesn't work, because buildObj isn't returning an object, but
an array of objects.

If you would do something like the following, it should work fine:

$obj[0]->foo = 'bar';
JW

Jul 17 '05 #5

"jerrygarciuh" <de*****@no.spam.nolaflash.com> wrote in message
news:rNg6e.967$H53.934@lakeread05...

"jerrygarciuh" <de*****@no.spam.nolaflash.com> wrote in message
news:IDg6e.965$H53.255@lakeread05...
"Janwillem Borleffs" <jw@jwscripts.com> wrote in message
news:42**********************@news.euronet.nl...
jerrygarciuh wrote:
I would like to add a field or two under certain circumstances. I
have tried

$obj->newfield = 'xxx';
$obj->{'newfield'} = 'yyy';
Both of these should work fine, do you get any error messages?

It would also be useful to see some of the code that generates the
initial object.
JW

JW,

Thank you for your rapid reply! Here is the function that builds the
objects from mySQL records. I am thinking my problem is actually caused
by using foreach on the array of objects and in PHP 4 I can't pass
reference like foreach ($arr as &$v).

Any advice most welcome!

jg

function &buildObj($result, $classname) {
if ($result) {
while($row = mysql_fetch_assoc($result)) {
if ($row === null) return null;

/* Create the object */
$obj =& new $classname;

/* Explode the array and set the objects's instance
data */
foreach($row as $key => $value)
{
$obj->{$key} = $value;
}
$objs[] = $obj;
}
return $objs;
} // end if ($result)
}


My tests confirm that my problem is that foreach operates on a copy and i
need to pass by reference or use while().


function forDate($date) { // $date = Y-m-d
$shelters = $this->retrieve("facility = '$_SESSION[facility]'");
$srObj = new ShelterRental;
if ($shelters) {
while ($s = current($shelters) ){
// retrieve today's rentals by shelter
$where = array("date = '$date'", "shelter = '$s->id'");
$rentals = $srObj->retrieve($where);
if ($rentals) {
echo $s->id;
foreach ($rentals as $r) {
$s->renter = $r->renter;
$s->contact = $r->contact;
$s->rentalid = $r->id;
} // foreach
} // if
$rentalInfo[] = $s;
next($shelters);
} // while
} // if
return $rentalInfo;
} // end forDate()
Jul 17 '05 #6

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

Similar topics

1
by: Anand | last post by:
Hi i am having trouble adding a recordset into the access database, the code seems to be working fine it passs and parses through all variables just fine without showing any errors and also when i...
2
by: Robin S. | last post by:
This is an "Add product" form. The user will enter a ProductNo (catalog number), select a Product Class (from cascading combo boxes) and then click a button to create the product. When a...
2
by: brenda.stow | last post by:
error msg " An error occured while referencing the object. You tried to run a visual basic procedure that improperly references a property or method of an object" This msg occurs everytime I add a...
0
by: choyk1 | last post by:
I intended to save properties of an object to a Hashtable. In this case, keys and values are not fixed types and I cannot use SortedList. I have no idea what order the .NET framework add items to...
2
by: Viorel | last post by:
Adding new row with default values. In order to insert programmatically a new row into a database table, without direct "INSERT INTO" SQL statement, I use the well-known DataTable.NewRow,...
12
by: Art | last post by:
Hi everyone I was hoping someone might be able to help me with this. I'm just starting to try to work with MS Access tables through VB.net. In Access I can take an existing table and add a new...
16
by: Geoff Jones | last post by:
Hi Can anybody help me with the following, hopefully simple, question? I have a table which I've connected to a dataset. I wish to add a new column to the beginning of the table and to fill...
5
by: Kosmos | last post by:
I have traveled the world and the seven seas and I have yet to come up with an answer to this question.... So I'm adding an attachment to an email from access... The following is the code: ...
1
by: vivekvt | last post by:
-------------------------------------------------------------------------------- here is the code to the asp page that adds data to the access database! <html> <body> <% Dim recordsaffected
1
by: swethak | last post by:
Hi, I am desiging the calendar application for that purpose i used the below code. But it is for only displys calendar. And also i want to add the events to calendar. In that code displys 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: 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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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,...
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.