Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Aug 2010
    Location
    Now Southern Oregon. I was born and had lived my life in Los Angeles until relocating last year (2010)
    Posts
    208
    Thanks
    52
    Thanked 1 Time in 1 Post

    referencing global variable in __construct()

    I am writing this code:
    Code:
    $_localMarkup = 'So, what is \'markup\'?';
    
    require_once('WC_php/pixViewerApp.php');
    
    class _PIX_VIEWER extends _VIEW
          {
           
           private static $_loaclMarkup = '';
           
           public function __construct()
                  {
                   self::$_localMarkup = $_localMarkup; // <<< ??
                  }
                  
           public function getAnswer()
                  {
                   return parent::subCLassTest(); //
                  }
                  
           public function getMarkup()
                  {
                   return self::$_localMarkup;
                  }
          }
    As you can see there is a global (defined in the file) named $_localMarkup.
    But the __construct() method will not read this variable.

    I initially thought that I could use the syntax
    Code:
    &$_localMarkup;
    but that does not seem to work either.

    Is there a way, without putting the $_localMarkup variable in a separate file
    and including it in the __construct() method?

    Thanks for time and attention
    JK

  • #2
    New Coder
    Join Date
    Feb 2012
    Posts
    40
    Thanks
    0
    Thanked 9 Times in 9 Posts
    You have to tell the function your using the global $_localMarkup and not $_localMarkup for the function scope.
    PHP Code:
           public function __construct()
                   global 
    $_localMarkup;
                  {
                   
    self::$_localMarkup $_localMarkup// <<< ??
                  

    and I just saw that:
    private static $_loaclMarkup = '';

    local is spelled wrong that could be your problem as well or part of your problem.
    Last edited by cercos; 02-25-2012 at 12:17 AM.

  • Users who have thanked cercos for this post:

    anotherJEK (02-26-2012)

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Avoid this whenever possible.
    Reformat your constructor to take a $_localMarkup and pass it in during construction. Global is a debugging nightmare and becomes worse in an object oriented environment.

  • Users who have thanked Fou-Lu for this post:

    anotherJEK (02-26-2012)

  • #4
    Regular Coder
    Join Date
    Aug 2010
    Location
    Now Southern Oregon. I was born and had lived my life in Los Angeles until relocating last year (2010)
    Posts
    208
    Thanks
    52
    Thanked 1 Time in 1 Post

    I saw the misspelling

    I saw the misspelling:
    Code:
    private static $_loaclMarkup = '';
    // should be
    private static $_localMarkup = '';
    I have a potential problem with passing this as an argument to the _PIX_VIEWER constructor: the parent class wants a constructor argument
    when _PIX_VIEWER is instantiated, so, I am thinking that _PIX_VIEWER
    will be faked out by seeing two arguments passed to it.
    Code:
    $_someVar = new _VIEW('argument')
    $_anotherVar = new _PIX_VIEWER();
    // complaint about _VIEW not getting an argument in the second line
    class _VIEW
          { /*various static variable defs*/
             public function __construct($_subDep)
                  {
                   self::$_subDep = $_subDep;
                   /* constructor code to set static var values*/
                 }
           }
    /*
    So, I would expect to instantiate _PIX_VIEWER as
    */
    $_yetAnotherVar = new _PIX_VIEWER('argument for parent class', 'argument for child class'); 
    // ????
    Thanks again for time and attention
    JK


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •