Page 1 of 1

Class/Object level issue (resolved, typo)

Posted: Thu Aug 11, 2011 1:06 pm
by kasuals
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().

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;
         }
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.

Re: Class/Object level issue

Posted: Thu Aug 11, 2011 5:08 pm
by kasuals
Not that I expect a fast response, but if anyone sees this post who can suggest an advanced forum for PHP I'd be willing to submit to it. It's not a huge deal, I'd just like to know if it's possible or not... and if not, it should be. It's just a pointer, unless I lost my way.

Re: Class/Object level issue

Posted: Thu Aug 11, 2011 5:11 pm
by Christopher
Not sure you should be doing what you are doing. The behavior of statics changed in 5.3 so see:

http://php.net/manual/en/language.oop5.static.php

Re: Class/Object level issue

Posted: Thu Aug 11, 2011 5:13 pm
by kasuals
Christopher wrote:Not sure you should be doing what you are doing. The behavior of statics changed in 5.3 so see:

http://php.net/manual/en/language.oop5.static.php
OK, perhaps that is what it is then. I just updated to 5.3 yesterday.

Thank you Christopher. I don't know if this is the solution, but I'll read the article and get back.

Re: Class/Object level issue

Posted: Thu Aug 11, 2011 8:50 pm
by kasuals
Thanks for the reply. I looked into it, doesn't really resolve any issues for me aside from the pseudo variable $this. I still can't tell why you can't access:

class::obj::property

with the singleton setup I posted above. I can accept if it's a limitation in PHP.

*edit*

I did a var_dump and got the following:

object(ErrorHandler)#2 (3) {
["ErrorText"]=>
string(21) "Unknown database 'dd'"
["Filename":protected]=>
string(32) "/var/www/html/goldiKB/submit.php"
["LogFile":protected]=>
string(9) "error.log"
}


This means $LogFile is set, now how would I go from self to $Instance->LogFile without assigning to a variable to reference from?

Re: Class/Object level issue

Posted: Thu Aug 11, 2011 9:17 pm
by Christopher
kasuals wrote: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
Not sure why you need the calls to Log() and Kill(). Usually a Singleton would just be:

Code: Select all

        //Singleton Pattern, die() if attempting multiple ErrorHandlers to force ErrorHandler::Kill()
        public static function getInstance($filename) {
                 if(! isset(self::$Instance)) {
                        self::$Instance = new ErrorHandler($filename);
                 }

                 return self::$Instance;
         }

Re: Class/Object level issue (resolved)

Posted: Thu Aug 11, 2011 9:35 pm
by kasuals
I figured it out. self::$Instance->LogFile will work, when I rewrote it in the post I had missed the > in my code but got it right in the example. Hooray for syntax blindness.

I feel stupid. Thanks for your help anyway.

Re: Class/Object level issue

Posted: Thu Aug 11, 2011 9:37 pm
by kasuals
Christopher wrote:
kasuals wrote: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
Not sure why you need the calls to Log() and Kill(). Usually a Singleton would just be:

Code: Select all

        //Singleton Pattern, die() if attempting multiple ErrorHandlers to force ErrorHandler::Kill()
        public static function getInstance($filename) {
                 if(! isset(self::$Instance)) {
                        self::$Instance = new ErrorHandler($filename);
                 }

                 return self::$Instance;
         }
The reason I have those in there was to play around with how I could affect it. They wont be there, and the function actually doesn't look like that. I just rewrote it from memory. I'm just trying to break things, it's nice to know what you can and can't get away with sometimes.