Member Avatar for LastMitch

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!

You forgot to create a new object from the candy class.

$MnM = new candy('My favourite candy here');
commented: Thanks for the explanation and solution! +5
Member Avatar for LastMitch

@pritaeas

Thanks for replying and explanation about a new object.

$MnM = new candy('My favourite candy here');

I will make some adjustment.

Member Avatar for diafol

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();
commented: Thanks for the examples & explaining solution! +0
Member Avatar for LastMitch

@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.

Member Avatar for diafol

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.

Member Avatar for LastMitch

@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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.