Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Apr 2003
    Location
    Canada, Ontario, Mississauga
    Posts
    312
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Undefined variable

    file called Form.php
    PHP Code:
    <?php
    class Form{
      var 
    $myString;
      function 
    Form($toPage){
        
        
    $myString "<form action=$toPage method=\"POST\">";
      }
      
      
      function 
    createTextBox($boxLabel "My Text Box"$boxName "TextBox"$boxSize 20
      
    $newLine false)
      {
        if(
    $newLine){
          
    $myString $myString "<br />";
        }
        
    $myString $myString $boxLabel "<INPUT TYPE=\"text\" NAME = \""$boxName "\" SIZE = \"" $boxSize "\">";
      }
      
      function 
    createTextArea($areLabel "My Text Area"$areaName "TextArea"
      
    $areaRows 4$areaCol 50$newLine false){
        if(
    $newLine){
          
    $myString $myString "<br />";
        }
        
    $myString $myString $areaLabel "<TEXTAREA NAME = \""$areaName " ROWS = \"" 
        
    $areaRows "\" COLS = \"" $areaCol "\" WRAP> </TEXTAREA>";
      }
      function 
    showForm(){
        echo 
    $myString "</form>";
      }
    }
    ?>
    file called Test.php
    PHP Code:
    <?php
    include 'Form.php';

      
    $frm = new Form("Hey");
      
    $frm->createTextBox();
      
    $frm->showForm();
    ?>
    this is the error I got


    Notice: Undefined variable: myString in C:\Servemtv-servemp3\htdocs\Class\Form.php on line 16

    Notice: Undefined variable: myString in C:\Servemtv-servemp3\htdocs\Class\Form.php on line 28

    I don't know why................................................????

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    to access a class variable (and indeed methods) you need to do so via '$this'
    e..g wherever you have $mystring use $this->mystring;


    PHP Code:
    <?
    error_reporting
    (E_ALL);
    class 
    Form
      var 
    $myString
      function 
    Form($toPage){ 
        
    $this->myString "<form action=$toPage method=\"POST\">"
      } 
       
       
      function 
    createTextBox($boxLabel "My Text Box"$boxName "TextBox"$boxSize 20,  
      
    $newLine false
      { 
        if(
    $newLine){ 
          
    $this->myString .= "<br />"
        } 
        
    $this->myString .= $boxLabel "<INPUT TYPE=\"text\" NAME = \""$boxName "\" SIZE = \"" $boxSize "\">"
      } 
       
      function 
    createTextArea($areLabel "My Text Area"$areaName "TextArea",  
      
    $areaRows 4$areaCol 50$newLine false){ 
        if(
    $newLine){ 
          
    $this->myString .= "<br />"
        } 
       
    $this->myString .= $areaLabel "<TEXTAREA NAME = \""$areaName " ROWS = \"" .  
        
    $areaRows "\" COLS = \"" $areaCol "\" WRAP> </TEXTAREA>"
      } 
      function 
    showForm(){ 
        
    $this->myString .= "</form>"
        echo 
    $this->mystring;
      } 

    ?>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    Regular Coder
    Join Date
    Apr 2003
    Location
    Canada, Ontario, Mississauga
    Posts
    312
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you for you help.

    One more question, how do I pass an object TextBox (textbox has it's own class) to Form (the form class)

    good oop practice is seperate them properly.

    in java

    frm = new Form (new TextBox("textName"));

    how to do it in php?

    thanx again

  • #4
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    Quote Originally Posted by firepages
    to access a class variable (and indeed methods) you need to do so via '$this'
    e..g wherever you have $mystring use $this->mystring;
    Unless of course it's static mr. firepages..
    Omnis mico antequam dominus Spookster!

  • #5
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    Unless of course it's static mr. firepages..
    not applicable in the object instance above


    Quote Originally Posted by weronpc
    in java frm = new Form (new TextBox("textName"));
    same same

    PHP Code:
    <?
    class form{
      function 
    form$str , &$textbox_obj){ //reference not required in PHP5 !//
          
    $this->textbox_obj $textbox_obj ;
          echo 
    $this->textbox_obj->display();
      }
    }
    $txtBx = new TextBox("textName");
    $frm = new Form"Hey" $txtBx ) ; 
    ?>
    Last edited by firepages; 04-24-2004 at 08:59 AM.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #6
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    Confusion is my middle name!
    Omnis mico antequam dominus Spookster!

  • #7
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    Quote Originally Posted by Mhtml
    Confusion is my middle name!
    doh , sorry, I just editied my post in case it came across the wrong way , twas meant in jest anyway !
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #8
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    haha, I think my statement has more effect now ..
    Omnis mico antequam dominus Spookster!

  • #9
    Regular Coder
    Join Date
    Apr 2003
    Location
    Canada, Ontario, Mississauga
    Posts
    312
    Thanks
    0
    Thanked 0 Times in 0 Posts
    anyone know why this doesn't work?

    //this is line 19
    PHP Code:
    echo tblContact->createCells(2, array(=> "User name: "=> "frmObj"),
     array(
    => 150=> 350), "top"); 
    //tblContact is an object of a table class



    the error I got is:
    Parse error: parse error, unexpected T_OBJECT_OPERATOR, expecting ',' or ';' in C:\htdocs\contact.php on line 19

    PHP Code:
    function createCells($count 2$displayText = array(0=>""1=>""), 
      
    $width = array(=> 250=> 250), $align "top"){} 
    it doesn't like this too much: echo tblContact->createCells();
    Last edited by weronpc; 04-26-2004 at 07:22 PM.

  • #10
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Could it be that you need a $ before tblContact ? ($tblContact->blah)

  • #11
    Regular Coder
    Join Date
    Apr 2003
    Location
    Canada, Ontario, Mississauga
    Posts
    312
    Thanks
    0
    Thanked 0 Times in 0 Posts
    oh my, so little error cause me so much trouble, thanx missing score


  •  

    Posting Permissions

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