Results 1 to 5 of 5

Thread: [RESOLVED] Issue with Access to Private Static Method

  1. #1

    Thread Starter
    Hyperactive Member tomcatexodus's Avatar
    Join Date
    Feb 2001
    Posts
    372

    Resolved [RESOLVED] Issue with Access to Private Static Method

    I'm working on a simple Template class (Mucklate) and I'm having a visibility problem with one of the methods. How can I allow the anonymous callback function for ob_start to access the parseTemplate method if I make the parseTemplate private static? I would rather it be invisible outside the class. Any other way I can achieve this?

    (This isn't the whole class, and it's still in progress, so if it looks odd, that's why.)
    PHP Code:
            public function __construct($templatePath$stripWhitespace false){

                if(
    $templatePath && file_exists($templatePath)){
                    
                    
    ob_start(function($buffer){ return Mucklate::parseTemplate($buffer); });
                    
                    include(
    $templatePath);
                                
                }
                
            }
                    
            public static function 
    parseTemplate($buffer){
                
                foreach(
    Mucklate::$varSet as $var){
                                    
                    
    $buffer str_replace(Mucklate::$delimiter $var["var"] . strrev(Mucklate::$delimiter), $var["value"], $buffer);
                    
                }
                
                return 
    $buffer;
                
            } 
    IWS

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Issue with Access to Private Static Method

    after much digging and experimenting, I don't think you can. but, ob_start() doesn't take a function as a parameter in the first place -- it takes a function's name. you'd need to specify the class (for static)/object (for non-static) and method in an array if you want to use a method for ob_start():
    PHP Code:
    //static methods:
    ob_start(array('MyClass''parseTemplate'));

    //non-static methods:
    ob_start(array($this'parseTemplate')); 
    however, as far as I can figure out, there's no way to provide a private method as the callback function for ob_start(). for example, this class works as expected:
    PHP Code:
    <?php
      
    class Test {
        
        public function 
    __construct(){
          if(
    ob_start(array('Test''parseTemplate'))){
            echo 
    "assigned output buffer";
          }else{
            echo 
    "couldn't assign output buffer";
          }
        }
      
        public static function 
    parseTemplate($buffer){
          return 
    __CLASS__  $buffer;
        }
        
      }
      
      
    $t = new Test();
    ?>
    the output is:
    Code:
    Testassigned output buffer
    however, once the access modifier for parseTemplate() is changed to private (static or non-static), the script simply dies.

    I don't know of any workaround for this.

  3. #3

    Thread Starter
    Hyperactive Member tomcatexodus's Avatar
    Join Date
    Feb 2001
    Posts
    372

    Re: Issue with Access to Private Static Method

    Quote Originally Posted by kows View Post
    however, once the access modifier for parseTemplate() is changed to private (static or non-static), the script simply dies.
    It is strange the way it dies too. Perhaps because of the output buffering, but I don't even get a error returned regarding the visibility issue. Unfortunate too though.
    IWS

  4. #4
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Issue with Access to Private Static Method

    Quote Originally Posted by tomcatexodus View Post
    It is strange the way it dies too. Perhaps because of the output buffering, but I don't even get a error returned regarding the visibility issue. Unfortunate too though.
    You will get an error, if you send the errors to an error log. It will occur on line zero in unknown

    I remember back in the early days of v5, visibility was very inconsistent. But it is worth noting that the call to the output buffer callback occurs outside the scope of the class so I would not have expected it to work. If it is really necessary, you can use reflection to verify the method which called the function, but is it worth it?
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [RESOLVED] Issue with Access to Private Static Method

    The general problem is that PHP doesn't have proper function pointers. Your private method is being called by name and so the scope resolution occurs from the foreign class. In other languages you can pass a function pointer which can be called from anywhere regardless of whether or not its target is visible from the point where it is called.

    An alternative approach to this specific case is to parse the buffer in the destructor of the template class, rather than using a callback.

    PHP Code:
    <?php
      
    class TestParser
      
    {
        public function 
    __construct()
        {
          
    ob_start();
        }
        
        public function 
    __destruct()
        {
          
    $data ob_get_contents();
          
    ob_clean();
          
          
    $data strrev($data);  // do your parsing here
          
    echo $data;
          
          
    ob_end_flush();
        }
      }
      
      
    $parser = new TestParser();

    ?>Hello world, foo bar baz and all that
    This approach allows you to make your parsing function private.
    Last edited by penagate; Apr 13th, 2010 at 02:38 AM.

Posting Permissions

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



Click Here to Expand Forum to Full Width