SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict
    Join Date
    Oct 2005
    Posts
    288
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    __toString or not __toString

    I'm missing a fundamental concept. The symptom is, despite using __toString, I still get "object #2" when I echo $signIn.

    I apologize for the amount of code included here, but I don't know where to focus.

    I have a factory pattern that decides which GUI I want:
    ...
    switch ($pageId) {
    case ("sign in"):
    switch ($action) {
    ...
    default:
    $signIn = new offbook_cls_SignInForm();
    $condition = $signIn->FormOnly();
    $this->SetNext($condition, "Sign In", "retrieve", "Sign In", "default");
    echo $signIn;
    break;
    }
    break;
    case ("register"):
    ...
    offbook_cls_SignInForm extends an abstract offbook_cls_Presentation which contains, among others, one __toString and two abstract methods.
    abstract class offbook_cls_Presentation{
    protected $docType; // Object containing <!DOCTYPE clause
    protected $openHTML; // object containing <html... clause
    protected $header; // object containing <head>...</head> (delegated)
    protected $body; // object containing <body>...</body> (delegated)
    protected $closeHTML; // object containing </html>
    ...
    abstract function SetHeader(); //delegates to offbook_cls_Heading object
    public function GetHeader() {
    return $this->header;
    }

    abstract function SetBody(); //delegates to offbook_cls_Body child
    public function GetBody() {
    return $this->body;
    }
    ...
    public function _toString(){
    $html = GetDocType() . "\n";
    $html .= GetOpenHTML() . "\n";
    $html .= GetHeader() . "\n";
    $html .= GetBody() . "\n";
    $html .= GetCloseHTML() . "\n";
    return $html;
    }
    Here's offbook_cls_SignInForm (initiated from the factory)
    <?php
    class offbook_cls_SignInForm
    extends offbook_cls_Presentation {

    public function SetHeader(){
    $head = new offbook_cls_Heading("Sign In Form");
    $head->SetStyle("index.css", "text/css"); // add custom style sheet
    return $head;
    }

    public function SetBody(){
    return "<p>SignIn Body</p>";
    }

    public function PopulateForm(){
    echo "Populate Form";
    return $true;
    }

    public function FormOnly(){
    $heading = $this->SetHeader(); // populate parent
    $body = $this->SetBody(); // populate parent
    return true;
    }
    }
    ?>
    and finally, an excerpt from offbook_cls_heading
    Class offbook_cls_Heading{
    ...
    function SetStyle($url_p, $type_p) {
    // e.g. SetStyle("base.css", "text/css"
    $this->styles[$url_p] = $type_p; // types can be the same, URLs aren't
    }
    ...
    function __toString() {
    $html = "<head>";
    ...
    $html .= " <title>'$this->title'</title>\n";
    ...
    foreach($this->styles as $url=>$type) {
    $html .= " <link rel=\"stylesheet\" type=\"'$type'\" href=\"offbook/'$url'\" />\n";
    }
    foreach($this->scripts as $url=>$type) {
    $html .= " <script type=\"'$type'\" src=\"'$url'\"> </script>\n";
    }
    ...
    $html .="</head>\n";
    return $html;
    }
    Again what I get is "object #2" when I echo $signIn.

  2. #2
    SitePoint Addict
    Join Date
    Oct 2005
    Posts
    288
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I found one problem in the presentation parent class,
    the __toString had only one leading underscore. Once I fixed that, i found that each of the "get" functions required a $this->.

    The result, though different, is still missing the heading and body parts:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">


    </html>


Bookmarks

Posting Permissions

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