Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

PHP Problem

Name: Anonymous 2013-03-15 19:07

class html {
    private static $content;

    public static function a($p) {
        self::$content &= $p."\n";
    }
   
    public static function an($p) {
        if($a->admin) {
            self::$content &= $p."\n";
        }
    }
   
    public static function write() {
        return self::$content;
    }
}

html::a("test");
echo html::write();

0

Why it does not work?

Name: Anonymous 2013-03-15 19:09

Why it does not work?
PHP

Name: Anonymous 2013-03-15 19:12

Yes, PHP is a problem.

Name: Anonymous 2013-03-15 19:13

That solves my problem, thank you very much

Name: Anonymous 2013-03-15 19:22

public class HTML
{
    private static String content="";

    public static void setHTMLContent(String s)
    {  
        content = content + s + "\n";
    }  

    public static String getHTMLContent()
    {  
        return content;
    }  

    public static void main(String[] args)
    {  
        HTML.setHTMLContent("test1");
        HTML.setHTMLContent("test2");
        System.out.println(HTML.getHTMLContent());
    }  
}


$ javac HTML.java; java HTML
test1
test2

$

Name: Anonymous 2013-03-15 19:22

Name: Anonymous 2013-03-15 19:30

>>6
Why do you do this?

Name: Anonymous 2013-03-16 5:54

You need the singleton pattern for this. I found this on PHP.net:


// saves content safely into an internal id.
// I would like to have endfunction :(
// it is so much more understandable.
// https://bugs.php.net/bug.php?id=24100
function set_content($text){
   static $id;
   if(!$id):
       $id = uniqid();
       // set safe id
       get_content($id);
   endif;
   $GLOBALS{$id} = $text;
  
}

// retrieves the content from the internal id.
function get_content($sid = false){
   static $id;
   if($sid && !$id): // we received save id. So we not get overwritten.
          $id = $sid;
       return "";
   endif;
   return $id ? $GLOBALS{$id} : "";
}

// html append. Add \n for readabiliy.
function html_a (){
    $args = func_get_args();
    set_content(get_content() . $args{0} . "\n");
}

function html_an(){
   if($a->admin):
    $args = func_get_args();
    set_content(get_content() . $args{0} . "\n");
   endif;
}

function html_write(){
    return get_content();
}

// Singleton pattern for html builder.
class html {
   protected function __construct(){
   }
  
   public static function create(){
           static $inst;
           if (!$inst):
              $inst = new html();
           endif;
           return $inst;
   }
  
   public $funcs = array ( "write" => "html_write",
                           "a" => "html_a",
                           "an" => "html_an");
   public function __call($met, $args){
          return call_user_func_array($this->funcs{$met}, $args);
   }
}

$html = html::create();
$html->a("test");
print $html->write();

Don't change these.
Name: Email:
Entire Thread Thread List