Page 1 of 1

Issue with Access to Private Static Method

Posted: Fri Apr 09, 2010 8:19 am
by tomcatexodus
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.)

Code: Select all

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

Re: Issue with Access to Private Static Method

Posted: Fri Apr 09, 2010 9:18 am
by dejvos
I am afraid of you can't allow anonymous access to private method.
I think, you have two possibilities:
a) leave method public
b) don't use ob_start() function.

Re: Issue with Access to Private Static Method

Posted: Fri Apr 09, 2010 9:43 am
by tomcatexodus
Thankyou for your quick response.

So that is in fact the case.. Hmm.. Are there any workarounds that I could consider aside from what you mentioned? My goal is only to prevent parseTemplate from being visible outside the class.

Re: Issue with Access to Private Static Method

Posted: Fri Apr 09, 2010 10:07 am
by ell0bo
Interesting code...

drop the ob_start and the anonymous function call. That will allow you to make function private again. Why are you doing what you're doing exactly though?

I'm going to guess your code you include grabs the current object and tries to use that for some kind of processing? I would consider this a bad design personally, but I'm sure you have a good reason for it. Instead of using the ob construct, create a global variable your script expects to exist, but personally I would add your include as another private / public static method of that class.

When doing OO, you really want to say away from globals as a GENERAL rule (yea, it's ok to break it sometimes), and if you're using the ob construct in an OO design to capture the return of a private function, something is definitely skewed.

Re: Issue with Access to Private Static Method

Posted: Sat Apr 10, 2010 11:20 pm
by tomcatexodus
It's a templating system.

You create an HTML/PHP/etc. file as a template. The PHP file you create (which is the actual file to be accessed in browsing) instantiates Mucklate and in the constructor names the template to be used. The template contains simple text references using a delimiter (Eg: !@main_content@! or !@news_feed@!) that are replaced by using the Mucklate method addVar() (Eg: addVar("news_feed", $news_feed_content); in this example perhaps $news_feed_content is the formatted return from a db)

The reason I use ob_start() with a callback is because I want to include the template and parse the buffered output, not just parse the file line by line. If the template contains PHP code, I want it to be executed. That way things like this are possible:

Code: Select all

<body>
!@main_content@!
<?php
    if($_SESSION["someVar"]){
        echo "!@more_content@!";
    }
?>
</body>
Anyways, I understand it may not be the most optimal approach, and that much more successful templating/MVC options exist, but I'm working my way up :)