468,247 Members | 1,323 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,247 developers. It's quick & easy.

Strange warning when trying to create a class

Hi,

Using PHP 4, I'm trying to create a class, and I get warnings in my
constructor function like the one below:

Notice: Undefined variable: m_cat_to_buy in
/usr/local/apache/htdocs/myfiles/dc_fns.php on line 72

Here is the code for my class:

class CProductForFree {
var $m_cat_for_free = "";
var $m_sub_cat_for_free = "";
var $m_style_for_free = "";
var $m_color_for_free = "";
var $m_size_for_free = "";
function CProductForFree($p_cat_for_free,
$p_sub_cat_for_free,
$p_style_for_free,
$p_color_for_free,
$p_size_for_free)
{
$this->$m_cat_for_free = $p_cat_for_free;
// line 72
$this->$m_sub_cat_for_free =
$p_sub_cat_for_free;
$this->$m_style_for_free = $p_style_for_free;
$this->$m_color_for_free = $p_color_for_free;
$this->$m_size_for_free = $p_size_for_free;
} // CProductForFree
} // CProductForFree

Why is the warning appearing and what can i do to correct it?

Thanks, - Dave

Mar 9 '06 #1
2 1084

la***********@zipmail.com wrote:
Hi,

Using PHP 4, I'm trying to create a class, and I get warnings in my
constructor function like the one below:

Notice: Undefined variable: m_cat_to_buy in
/usr/local/apache/htdocs/myfiles/dc_fns.php on line 72

Here is the code for my class:

class CProductForFree {
var $m_cat_for_free = "";
var $m_sub_cat_for_free = "";
var $m_style_for_free = "";
var $m_color_for_free = "";
var $m_size_for_free = "";
function CProductForFree($p_cat_for_free,
$p_sub_cat_for_free,
$p_style_for_free,
$p_color_for_free,
$p_size_for_free)
{
$this->$m_cat_for_free = $p_cat_for_free;
// line 72
$this->$m_sub_cat_for_free =
$p_sub_cat_for_free;
$this->$m_style_for_free = $p_style_for_free;
$this->$m_color_for_free = $p_color_for_free;
$this->$m_size_for_free = $p_size_for_free;
} // CProductForFree
} // CProductForFree

Why is the warning appearing and what can i do to correct it?

Thanks, - Dave


Tough to tell since you didn't include the code that's actually causing
the error. I suspect it has something to do with whatever is on line
72 of /usr/local/apache/htdocs/myfiles/dc_fns.php.

Mar 9 '06 #2
On Wed, 08 Mar 2006 19:23:28 -0800, la***********@zipmail.com wrote:
Using PHP 4, I'm trying to create a class, and I get warnings in my
constructor function like the one below:
I'm guessing it's an easy one....
Notice: Undefined variable: m_cat_to_buy in
/usr/local/apache/htdocs/myfiles/dc_fns.php on line 72
m_cat_to_buy doesn't appear in your snippet of the class, so it's hard to
100% say, but there are some problems in your code that make it seem
likely that I know what you've done.
class CProductForFree {
var $m_cat_for_free = "";
Here you correctly declare a member variable m_cat_for_free
function CProductForFree($p_cat_for_free, [snip])
{
$this->$m_cat_for_free = $p_cat_for_free;


Here you assign a passed in variable ($p_cat_for_free) to a dynamically
determined property which is not determined. An example would make this
clearer I'm sure:

You could have used

function CProductForFree($p_cat_for_free, [snip])
{
$property = "m_cat_for_free";
$this->$property = $p_cat_for_free;

But what you likely meant was:

function CProductForFree($p_cat_for_free, [snip])
{
$this->m_cat_for_free = $p_cat_for_free;

You create the member variable with "var $name" but you use it with
"$this->name" (notice no $ before name).

So, although you haven't included m_cat_to_buy in your code snippet, I'm
guessing it's the same problem as this.

Hope this helps.

Cheers,
Andy

--
Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
http://www.gphpedit.org | PHP editor for Gnome 2
http://www.andyjeffries.co.uk | Personal site and photos

Mar 9 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Sony Antony | last post: by
2 posts views Thread by Rhino | last post: by
1 post views Thread by billa1972 | last post: by
2 posts views Thread by Robbie Hatley | last post: by
8 posts views Thread by mantrid | last post: by
1 post views Thread by stromhau | last post: by
6 posts views Thread by tony | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.