PHP OOP - Defining a property from inside a function

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

User avatar
abrenecki
Posts: 62
Joined: Mon Sep 10, 2007 8:39 am UTC
Location: Mid North SA, Australia

PHP OOP - Defining a property from inside a function

Postby abrenecki » Thu Dec 13, 2007 6:38 am UTC

This is a bit of an OOP newbie question.
I have the following code inside a function called loadby, within a class called dbObj. It's esssentially a multipurpose object for MySQL DBs.
Anyways, $keys contains the column names in an array, $data the data for one row in an associative array, and I want to put each one into a property. So, I'm doing this:

Code: Select all

      foreach($keys as $key){
         $this->$key = $data[$key];
      }

Which works, but generates half a ton of E_NOTICEs because the keys weren't defined already.

Code: Select all

8 (Undefined property: title) at /[path]/dbobj.php:61

Note: this is my custom error handler thingy; it goes "$code ($msg) at $file:$line" for anyone who thinks this looks odd.
How can I define them from within this code?
Ooh, before I forget, this is PHP4 :shock:

User avatar
OOPMan
Posts: 314
Joined: Mon Oct 15, 2007 10:20 am UTC
Location: Cape Town, South Africa

Re: PHP OOP - Defining a property from inside a function

Postby OOPMan » Thu Dec 13, 2007 12:25 pm UTC

PHP4? Ouch...
Image

Image

User avatar
Tei
Posts: 63
Joined: Fri Nov 30, 2007 2:58 pm UTC

Re: PHP OOP - Defining a property from inside a function

Postby Tei » Thu Dec 13, 2007 2:53 pm UTC

Whats the problem?
Maybe you can try:
@$this->$key = $data[$key];

Or, something like:

$priorLevel = fakegetCurrentErrorLevel();
fakeSetErrorLevel( $priorLevel & ~(E_NOTICE) );//is "~" or "!"?
$this->$key = $data[$key];
fakeSetErrorLevel( $priorLevel );

Anyway ...
PHP4 is not happy, you are tryiing to add syntas sugar to the game.

User avatar
abrenecki
Posts: 62
Joined: Mon Sep 10, 2007 8:39 am UTC
Location: Mid North SA, Australia

Re: PHP OOP - Defining a property from inside a function

Postby abrenecki » Fri Dec 14, 2007 12:09 am UTC

Neither works - I'm using a custom error handler and can't seem to use global variables to control that either - nor does sticking var in there.
Edit: I've given up, I'm just suppressing E_NOTICEs.
Edit: Here's my hack, from within my error handler, on the line that echos the error message:

Code: Select all

if ($code != E_NOTICE || ($mode != 'production' && !substr('dbobj.php',$file))) echo $emsg;

Besides, surely the original code isn't much worse than variable variables.

Ambush Commander
Posts: 18
Joined: Thu Dec 13, 2007 8:57 pm UTC

Re: PHP OOP - Defining a property from inside a function

Postby Ambush Commander » Fri Dec 14, 2007 1:46 am UTC

The reason why the shutup operator wasn't working was because your custom error reporter doesn't honor error_reporting(); @ works by temporarily setting that to 0.

As far as I can tell,

Code: Select all

<?php
$obj = new stdclass;
$prop = 'foo';
$obj->$prop = 'value';
?>


Shouldn't emit any errors. Only when you have a stray $obj->$prop will it complain about undefined properties.
HTML Purifier - Standards-Compliant HTML Filtering!

joeframbach
Posts: 1478
Joined: Sun Nov 05, 2006 12:49 am UTC

Re: PHP OOP - Defining a property from inside a function

Postby joeframbach » Fri Dec 14, 2007 2:13 am UTC

abrenecki wrote:PH POOP


hehehehehe..

POOP.


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 7 guests