Problem with a $this statement not working

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phil Latio

    #1

    Problem with a $this statement not working

    Can anyone tell me why:-

    class=\"$this->styleclass\"

    statement in the function generateHTML() (which is at very bottom) fails to
    pass the value "error" meaning I can't use the style class I defined?

    In the example below, I instantiate 3 objects and pass initial arguments to
    the constructor. Before displaying the form, I wish to pass a new argument
    to one of the objects. I tested it the syntax to ensure the assignment
    statement was correct using a simple print statement to confirm it had
    updated and indeed it had (this has been commented out below).

    Cheers

    Phil



    <?php

    $newform1 = new FormObject("POS T", "$PHP_SELF" );
    $newbox1 = new InputTextBox("f irst_name", "", "");
    $newbox2 = new InputTextBox("l ast_name", "", "");

    $html = "<html><hea d>";
    $html .= "<style type=\"text/css\">";
    $html .= ".error";
    $html .= "{";
    $html .= "color: #781351;";
    $html .= "background : #fee3ad;";
    $html .= "border: 1px solid #781351";
    $html .= "}";
    $html .= "</style>";
    $html .= "</head><body>";
    $html .= $newform1->formOpen();
    $html .= $newbox1->generateHTML() ;
    $html .= "<br />";
    $html .= $newbox2->generateHTML() ;
    $html .= "<br />";
    $html .= $newform1->inputSubmitBut ton();
    $html .= "<br />";
    $html .= $newform1->formClose();

    $newbox1->setStyleClass( "error");

    /*
    print $newbox1->setStyleClass( );
    */

    print $html;



    class FormObject
    {
    private $formAction;
    private $formMethod;

    function __construct($fo rmMethod, $formAction)
    {
    $this->formMethod = $formMethod;
    $this->formAction = $formAction;
    }

    function formOpen()
    {
    $html = "<form method=\"$this->formMethod\"
    action=\"$this->formAction\">" ;
    return $html;
    }

    function inputSubmitButt on()
    {
    $html = "<input type=\"submit\" value=\"Submit\ " />";
    return $html;
    }

    function formClose()
    {
    $html = "</form>";
    return $html;
    }
    }

    class InputTextBox
    {
    private $name;
    private $styleclass;
    private $value;

    function __construct($na me, $styleclass, $value)
    {
    $this->name = $name;
    $this->styleclass = $styleclass;
    $this->value = $value;
    }

    function setStyleClass($ styleclass)
    {
    $this->styleclass = $styleclass;
    }

    function getStyleClass()
    {
    return $this->styleclass;
    }

    function setValue($value )
    {
    $this->value = $value;
    }

    function getValue()
    {
    return $this->value;
    }

    function generateHTML()
    {
    $html = "<input type=text name=\"$this->name\" class=\"$this->styleclass\"
    value=\"$this->value\" />";
    return $html;
    }
    }
    ?>


  • Phil Latio

    #2
    Re: Problem with a $this statement not working

    I've just realised that:

    $newbox1->setStyleClass( "error");

    is after

    $html .= $newbox1->generateHTML() ;

    so class has no value when the generateHTML is run.

    Bugger.

    Cheers

    Phil


    Comment

    Working...