Re: using return statements in class __construct method?

From: Date: Mon, 03 Nov 2014 01:45:06 +0000
Subject: Re: using return statements in class __construct method?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-324026@lists.php.net to get a copy of this message
On Nov 2, 2014, at 5:20 PM, Jeffry Killen wrote:
I understand that it is not possible for the __construct method to return a value itself, but is it legal to statically call a method inside the __construct() method that statically calls another method that does use the return statement? __construct statically calls method a method a statically calls method b; does not use return but just sets object properties. method b returns values to method a I would think that this would be valid. I have posted the code prior to debugging at http://stackoverflow.com/questions/26705965/does-php-oop-allow-return-statements-in-construct-functions-in-this-way However the current partial code is this: class _UPDATER
     {
      private static $_fontList = array();
      private static $_fontDir = 'fonts';
      private static $_ttfDir = 'new-fonts-ttf';
      private static $_eotDir = 'new-fonts-eot';
      private static $_ttfSubDirs = array();
      private static $_fontIndex = "fontIndex.php";
      private static $_cssFileInventory = "chooserDeclarations.css";
      private static $_error = '';
      public function __construct()
             {
              self::gather();
             }
      public function getError()
             {
              if(!self::$_error)
                {
                 return 0;
                }
              $_error = self::$_error;
              self::$_error = '';
              return $_error;
             }
      private function gather()
              {
                $_error = '';
                self::$_error = 'test one';
               /* the rest of the method code is commented out, the above is all that is being run now.
                 etc....*/
              }
As a side effect of trying to debug this, the following is the code that loads and instantiates the class code $_file_self = basename($_SERVER['PHP_SELF']); $_error = ''; $_stat = ''; if($_POST['proc'] == 'go') { if(!file_exists('class/updaterClass.php'))
    {
     $_error = 'updater class file not found';
    }
else if(!is_readable('class/updaterClass.php'))
    {
     $_error = "updater class file is not readable";
    }
else
    {
     $_ret = '';
     require_once('class/updaterClass.php');
     $_update = new _UPDATER();
     $_ret = $_update->getError();
     print $_ret; // <<<< notice this
     if($_ret)
       {
        $_error = $_ret;
       }
     else
       {
        $_stat = 'update successfull';
       }
     header('location:clear.php'); // dummy file that redirects back to the http_referer to clear post vars.
    }
}
I just figured out that the call to header is the problem with this page. The redirect occurs and when the redirect back arrives, there is no value to display. So I put in a second hidden field that will be set by php with either the $_error or $_stat value. So in the top I can redirect IF there is a value being submitted for the new hidden field (to avoid reprocessing post vars during test and debugging and if the client is reloaded).

« previous php.general (#324026) next »