Jump to content

how to fix it ?

- - - - -

  • Please log in to reply
2 replies to this topic

#1
Preslav Panayotov

Preslav Panayotov

    Newbie

  • Members
  • Pip
  • 4 posts
<?php

class MyExceptions {


    private static $object = null;

    public $problem = array();


    private function __construct() {}


    public function setProblem($msg, $code) {

        self::getInstance();

        $this->problem = array('msg' => $msg, 'code' => $code);

    }


    public function getProblem() {

        self::getInstance();

        return $this->problem;

    }


    private function getInstance() {

        if (self::$object == null) {

            self::$object = new MyExceptions();

        }

    }


}


MyExceptions::setProblem("habibi",001);


var_dump(MyExceptions::getProblem());



Quote

Fatal error: Using $this when not in object context in D:\wamp\www\MyExceptions.php on line 11

Нещо не мога да я схвана тази грешка.

#2
SoN9ne

SoN9ne

    Programmer

  • Members
  • PipPipPipPip
  • 129 posts
Just have getInstance return the actual instance and use that instead of $this.
<?php

class MyExceptions {


	private static $object = null;

	public $problem = array();


	private function __construct() {

	}


	public function setProblem($msg, $code) {

		self::getInstance()->problem = array('msg' => $msg, 'code' => $code);

	}


	public function getProblem() {

		return self::getInstance()->problem;

	}


	private static function getInstance() {

		if (self::$object == null) {

			self::$object = new MyExceptions();

		}

		return self::$object;

	}


}

There are better ways for exception handling in PHP and this is not really the best method to handle exceptions... You should really extend the Exception object and use try/catch
"Life would be so much easier if we only had the source code."

#3
Preslav Panayotov

Preslav Panayotov

    Newbie

  • Members
  • Pip
  • 4 posts
Ty :love::love:




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users