...

View Full Version : Undefined variable



weronpc
04-24-2004, 06:18 AM
file called Form.php


<?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
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................................................????

firepages
04-24-2004, 06:44 AM
to access a class variable (and indeed methods) you need to do so via '$this'
e..g wherever you have $mystring use $this->mystring;




<?
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;
}
}
?>

weronpc
04-24-2004, 07:59 AM
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

Mhtml
04-24-2004, 08:32 AM
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..

firepages
04-24-2004, 08:53 AM
Unless of course it's static mr. firepages..

not applicable in the object instance above ;)



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

same same



<?
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 ) ;
?>

Mhtml
04-24-2004, 08:58 AM
Confusion is my middle name!

firepages
04-24-2004, 09:06 AM
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 !

Mhtml
04-24-2004, 09:45 AM
haha, I think my statement has more effect now ;)..

weronpc
04-26-2004, 07:05 PM
anyone know why this doesn't work?

//this is line 19

echo tblContact->createCells(2, array(0 => "User name: ", 1 => "frmObj"),
array(0 => 150, 1 => 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


function createCells($count = 2, $displayText = array(0=>"", 1=>""),
$width = array(0 => 250, 1 => 250), $align = "top"){}

it doesn't like this too much: echo tblContact->createCells();

missing-score
04-26-2004, 08:50 PM
Could it be that you need a $ before tblContact ? ($tblContact->blah)

weronpc
04-26-2004, 09:36 PM
oh my, so little error cause me so much trouble, thanx missing score



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum