Class/Object level issue (resolved, typo)
Posted: Thu Aug 11, 2011 1:06 pm
All my code works as is, I'd just prefer to not assign another variable a reference to the object if I can get away with it. It's using the Singleton design pattern so everything that needs to be done is done in getInstance. You can assume that I've properly defined everything in the private __construct().
Basically the object is initialized using:
$error = ErrorHandler::getInstance(__FILE__, $errortext);
This makes $error a valid object reference using the ErrorHandler class, and sets the static variable $Instance to hold the object itself. self::$Instance is constructed and defined, and now I want to access $LogFile inside self::$Instance.
I'm just asking why a class called upon itself with a static object created from the same class containing a property $LogFile can't be called directly, i.e.:
self::$Instance::$LogFile OR self::$Instance->Logfile
if the following conditions are true: Singleton instance was created, $Instance is a valid object and $LogFile is a defined property of $Instance.
The workaround I've found for this is $instance = self::$Instance and then accessing the property $LogFile in the object from the new reference $instance. ($instance->LogFile)
I just need to know if it's possible to access $LogFile without assigning $x = self::$Instance first.
I'll try to clean my question up a bit...
It's basically a syntax question, is it possible to access variables from (class)->(obj)->(property) without first referencing the object with a new variable. It seems like there is only 2 levels of access, the class and the object. I haven't found a way to access the property one level lower.
Code: Select all
//Singleton Pattern, die() if attempting multiple ErrorHandlers to force ErrorHandler::Kill()
public static function getInstance($filename, $errortext = "No error message provided.") {
if(self::$Instance) {
$instance = self::$Instance;
self::Log($instance->LogFile, "Cannot create multiple ErrorHandlers. To die use ErrorHandler::Kill()");
self::Kill();
} else {
self::$Instance = new ErrorHandler($filename, $errortext);
}
return self::$Instance;
}
$error = ErrorHandler::getInstance(__FILE__, $errortext);
This makes $error a valid object reference using the ErrorHandler class, and sets the static variable $Instance to hold the object itself. self::$Instance is constructed and defined, and now I want to access $LogFile inside self::$Instance.
I'm just asking why a class called upon itself with a static object created from the same class containing a property $LogFile can't be called directly, i.e.:
self::$Instance::$LogFile OR self::$Instance->Logfile
if the following conditions are true: Singleton instance was created, $Instance is a valid object and $LogFile is a defined property of $Instance.
The workaround I've found for this is $instance = self::$Instance and then accessing the property $LogFile in the object from the new reference $instance. ($instance->LogFile)
I just need to know if it's possible to access $LogFile without assigning $x = self::$Instance first.
I'll try to clean my question up a bit...
It's basically a syntax question, is it possible to access variables from (class)->(obj)->(property) without first referencing the object with a new variable. It seems like there is only 2 levels of access, the class and the object. I haven't found a way to access the property one level lower.