0

I've a script with variables in a class ($slug).

When I run this script, it works, but I get a warning: Warning: Creating default object from empty value. I know, I can hide this warnings in my php.ini, but how can ik fix this warning?

    $test = new myclass();
class myclass {
    private $order;

    public function __construct() {
        $this->order = (object) NULL;
    }
    public function setVars($slug, $number) {
        $this -> order -> $slug -> number = $number;
    }

    public function getVars($slug) {
        echo $this -> order -> $slug -> number;
    }
}
$test -> setVars($slug="hello", $number=5);
$test -> getVars($slug="hello");
2
  • 1) You are instanciating the object before the class definition. 2) Modify your method default value to: function setVars($slug, $number=5); Commented Sep 24, 2013 at 12:24
  • @rogeriolino, sorry: this doens't fix my problem... Commented Sep 24, 2013 at 12:34

1 Answer 1

0

Simply define order object as stdClass

public function __construct() {
    $this->order = new stdClass();
}


see this: Creating default object from empty value in PHP?

also this: http://www.php.net/manual/en/language.oop5.basic.php

Sign up to request clarification or add additional context in comments.

2 Comments

sorry: this doens't fix my problem... new stdClass() == (object)NULL

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.