What does 'property of non-object' mean?
Hi,
I want to know how did I came up with this error when I run my code:
Trying to get property of non-object in line 19.
What does it mean?
I want to echo out My favorite candy is M&M Peanuts.
But instead I echo out My favorite candy is.
Here is my code:
<?php
class candy {
public $MnM='M&M Peanuts';
public function __construct($MnM)
{
$this->MnM = $MnM;
}
public function get_MnM()
{
return $this->MnM;
}
}
echo 'My favorite candy is '. $MnM->MnM.'.';
?>
Any Suggestions and explanation will help. I appreciate it. Thank!
49 Minutes
Discussion Span
Related Article: Undefined Property and Non-Object Error
is a solved PHP discussion thread by joshmac that has 1 reply, was last updated 9 months ago and has been tagged with the keywords: framework, php, classes, namespaces.
LastMitch
Industrious Poster
4,181 posts since Mar 2012
Reputation Points: 132
Solved Threads: 335
Skill Endorsements: 45
You forgot to create a new object from the candy class.
$MnM = new candy('My favourite candy here');
pritaeas
Posting Prodigy
9,309 posts since Jul 2006
Reputation Points: 1,178
Solved Threads: 1,465
Skill Endorsements: 86
@pritaeas
Thanks for replying and explanation about a new object.
$MnM = new candy('My favourite candy here');
I will make some adjustment.
LastMitch
Industrious Poster
4,181 posts since Mar 2012
Reputation Points: 132
Solved Threads: 335
Skill Endorsements: 45
You haven't created an object - you need something like this:
<?php
class candy {
private $MnM = 'M&M Peanuts';
public function __construct($MnM = NULL)
{
if($MnM)$this->MnM = $MnM;
}
public function get_MnM()
{
return $this->MnM;
}
}
$candy = new candy();
echo 'My favoruite candy is ' . $candy->get_MnM();
echo "<br />";
$candy2 = new candy('Smarties');
echo 'My favoruite candy is ' . $candy2->get_MnM();
?>
The optional parameter in the __construct allows easy override of the default. However, you could do the same with a set method:
class candy {
private $MnM = 'M&M Peanuts';
public function __construct()
{
//no constructs??
}
public function get_MnM()
{
return $this->MnM;
}
public function set_MnM($candyname)
{
$this->MnM = $candyname;
}
}
$candy = new candy();
echo 'My favoruite candy is ' . $candy->get_MnM();
echo "<br />";
$candy2 = new candy();
$candy2->set_MnM('Smarties');
echo 'My favoruite candy is ' . $candy2->get_MnM();
diafol
Keep Smiling
10,665 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,514
Skill Endorsements: 57
@diafol
Thanks for replying and explanation about a new object and examples.
I was about to post an error regarding about the new object because I couldn't figure it out where to put this:
$MnM = new candy('My favourite candy here');
I will test out your code and make some adjustment.
LastMitch
Industrious Poster
4,181 posts since Mar 2012
Reputation Points: 132
Solved Threads: 335
Skill Endorsements: 45
Perhaps have another look at Pritaeas' excellent tut: http://www.daniweb.com/web-development/php/threads/437592/introduction-to-phps-object-orientation
//EDIT
Sorry guys - was typing when I saw the replies. :(
Anyway, just a note on encapsulation - try to limit the visibility of your properties - use private or protected whenever possible - you don't want client code accessing these props if they can mess up your class functionality.
I'm assuming you want to keep MandMs as your default value. This means every subsequent object created will have this as a default value unless set to otherwise. If you want to keep the last set value for subsequent objects, you can make the property ($MnM) static, although you'd have to change the way you get and set the property to something like this:
class candy {
private static $MnM = 'M&M Peanuts';
public function __construct()
{
//no constructs??
}
public function get_MnM()
{
return self::$MnM;
}
public function set_MnM($candyname)
{
self::$MnM = $candyname;
}
}
Not a great way maybe.
diafol
Keep Smiling
10,665 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,514
Skill Endorsements: 57
@diafol
@pritaeas
It work! I want to thank both of you for being patience and helping me out with this issue.
I did follow the guideline that pritaeas wrote. It's a lot of stuff to learn.
Thanks for the example.
I understand how the new object works. This is hard.
This is my old code:
<?php
class candy {
public $MnM='M&M Peanuts';
public function __construct($MnM)
{
$this->MnM = $MnM;
}
public function get_MnM()
{
return $this->MnM;
}
}
echo 'My favorite candy is '. $MnM->MnM.'.';
?>
Which has an error: Trying to get property of non-object
This is my new code:
<?php
class candy {
private $MnM = 'M&M Peanuts';
public function __construct()
{
//Without construct() function, it echo out My favorite candy is M&M Peanuts.
}
public function get_MnM()
{
return $this->MnM;
}
public function set_MnM($candy)
{
$this->MnM = $candy;
}
}
$candy = new candy();
echo 'My favoruite candy is ' . $candy->get_MnM().'.';
echo "<br />";
?>
There was no error. It echo My favoruite candy is M&M Peanuts.
LastMitch
Industrious Poster
4,181 posts since Mar 2012
Reputation Points: 132
Solved Threads: 335
Skill Endorsements: 45
Question Answered as of 6 Months Ago by
diafol
and
pritaeas