Problem with class constructor

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Charles Russell

    Problem with class constructor

    I am witting a project that needs to have unique names for each detail
    line. To achieve this I use the day and line number as part of the
    name. Given my code I would expect that I would get something like
    <label for='hrs'>Hours <input type='text' name='hrs1_1' style='width:2e m'
    /></label>. My result is <label for='hrs'>Hours <input type='text'
    name='hrs_' style='width:2e m' /></label> as you can see the name
    attribute does not have the variable portion. I can see that the
    parameters passed are comming in to the constructor correctly but the
    member variables of my class are not being set. I can't see why, it
    must be something real simple.

    I have declared the class as follows:

    class clsBlnkDetail
    {
    var $mTs;
    var $mDay;
    var $mLine;

    function clsBlnkDetail($ Day,$Line,$shee t="none")
    {
    //Debug code
    $fp=fopen("debu g.txt","w");
    //End Debug code

    if ($sheet != "none")
    {
    if($day > 6)
    {
    echo "There are only 7 days in a week";
    return(0);
    }

    //member variable should be set here
    $this->mDay=$Day;
    $this->mLine=$Line;
    $this->mTs=$sheet;

    //Debug code
    fwrite($fp,$thi s->$mDay." ".$this->$mLine);
    fclose($fp);
    //End Debugg code

    }
    }
    function MkName()
    {
    return($this->mDay."_".$th is->mLine);
    }
    function Build()
    {
    //echo "Blank detail line<br />";
    echo "<label for='hrs'>Hours ";
    $widgitName="hr s".$this->MKName();
    echo "<input type='text' name='$widgitNa me; style='width:2e m' />";
    echo"</label> <br />";
    }
    }

  • Brion Vibber

    #2
    Re: Problem with class constructor

    Charles Russell wrote:[color=blue]
    > Given my code I would expect that I would get something like
    > <label for='hrs'>Hours <input type='text' name='hrs1_1' style='width:2e m'
    > /></label>. My result is <label for='hrs'>Hours <input type='text'
    > name='hrs_' style='width:2e m' /></label> as you can see the name
    > attribute does not have the variable portion.[/color]
    [snip][color=blue]
    > if ($sheet != "none")
    > {
    > if($day > 6)[/color]
    ^^^^
    Don't forget that variable names are case sensitive. This test will not
    trigger when $Day > 6; the code will also produce a warning when
    error_reporting is set to E_ALL.
    [color=blue]
    > {
    > echo "There are only 7 days in a week";
    > return(0);
    > }
    >
    > //member variable should be set here
    > $this->mDay=$Day;
    > $this->mLine=$Line;
    > $this->mTs=$sheet;[/color]

    You're still in the first if() block, so if you didn't pass a non-"none"
    $sheet parameter, this will never be executed and your member variables
    will never be set.

    I do, however, get the expected results with your code when passing a
    parameter:

    $foo = new clsBlnkDetail( 1, 1, 'notblank' );
    $foo->Build();

    -- brion vibber (brion @ pobox.com)

    Comment

    Working...