NYCPHP Meetup

NYPHP.org

[nycphp-talk] OOP Error/Question

Andy Dirnberger dirn at dirnonline.com
Fri Jul 27 10:43:02 EDT 2007


Try self::$builtMessage = $errorStr;

 

Or another option is to adopt the singleton pattern.  This will give you the
same instance of the Error object anywhere you try to use it in your code.

 

Something like:

 

class Error {

  private function __constrct () {

  }

 

  public function GetInstance () {

    If (self::$instance == null) self::$instance = new self;

    Return self::$instance;

  }

 

  public function Backtrace () {

    // code from below

  }

 

  private $builtMessage;

  private static $instance = null;

}

 

And in your code:

 

$err = Error::GetInstance ();

$err->Backtrace ();

 

  _____  

From: talk-bounces at lists.nyphp.org [mailto:talk-bounces at lists.nyphp.org] On
Behalf Of Ben Sgro (ProjectSkyLine)
Sent: Friday, July 27, 2007 10:29 AM
To: NYPHP Talk
Subject: Re: [nycphp-talk] OOP Error/Question

 

Hello, 

 

Thanks, both your fixes fixed this issue.

 

However, it broke it in another place.

 

PHP Fatal error:  Using $this when not in object context in
/var/www/html/sk/OOPLIB/ERRO.php on line 80

Inside the Backtrace( ) function I have:

        $this->builtMessage = $errorStr;

 

To save the message to a string within the object. I can't do
self::builtMessage = $errorStr,

so what can I do?

 

Here's the backtrace function:

 

    static function Backtrace( )
    {
        $btSet  = debug_backtrace( );
        $iValue = 1; /* [0] is the traceback for the Error::
*///sizeof($btSet) - 1;
        $eSet   = $btSet[$iValue];
        
        $errorStr = "\ndate: " . date('r')
                  . "\nfile: " . $eSet['file']
                  . "\nline: " . $eSet['line']
                  . "\nfunction: " . @$eSet['class'] . '::' .
$eSet['function']
                  . "\nargs: ";

 

        /* Build a list of the functions arguments. */
        while(list($argIndex, $argStr) = each($eSet['args']))
        {
            $errorStr .= "[${argIndex}]$argStr ";
        }
        $this->builtMessage = $errorStr;
    }

 

Which is called from another function:

            case LOG_LEVEL_ALERT:
                self::Backtrace( );

 

- Ben

 

 

Ben Sgro, Chief Engineer
ProjectSkyLine - Defining New Horizons

----- Original Message ----- 

From: drydell at optonline.net 

To: NYPHP Talk <mailto:talk at lists.nyphp.org>  

Sent: Friday, July 27, 2007 10:17 AM

Subject: Re: [nycphp-talk] OOP Error/Question

 

you're calling Error statically, so there can't be any object instance
references... instead of $this->Backtrace(), use self::Backtrace();

----- Original Message -----
From: "Ben Sgro (ProjectSkyLine)" 
Date: Friday, July 27, 2007 10:10 am
Subject: [nycphp-talk] OOP Error/Question
To: NYPHP Talk 

> Good morning, 
> 
> So, I have a script where I'm calling:
> 
> function ReceivePOPEmail($popObject)
> {
> $error = $popObject->Login($popObject-
> >username,$popObject->password,
> $popObject->apop);
> if ( $error != '' )
> {
> /* We've had an error. */
> Error::Log("Error: " . HtmlSpecialChars($error), 
> LOG_LEVEL_ALERT); return PROC_FAILURE;
> }
> return PROC_SUCCESS;
> }
> 
> that's function ... I don't want to pass the Error Object 
> around, so I just want to call it via:
> Error:Log(.....);
> 
> Now, inside the Error class, there is this code:
> case LOG_LEVEL_ALERT:
> $this->Backtrace( );
> 
> Which is getting executed. But I'm getting the error:
> 
> PHP Fatal error: Using $this when not in object context in 
> /var/www/html/sk/OOPLIB/ERRO.php on line 101
> 
> So, how do I call that class's method w/out passing the object around?
> 
> - Ben
> 
> 
> Ben Sgro, Chief Engineer
> ProjectSkyLine - Defining New Horizons
> 
> This e-mail is confidential information intended only for the 
> use of the individual to whom it is addressed.
> 


  _____  


_______________________________________________
New York PHP Community Talk Mailing List
http://lists.nyphp.org/mailman/listinfo/talk

NYPHPCon 2006 Presentations Online
http://www.nyphpcon.com

Show Your Participation in New York PHP
http://www.nyphp.org/show_participation.php

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20070727/0df0965e/attachment.html>


More information about the talk mailing list