By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,890 Members | 1,048 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,890 IT Pros & Developers. It's quick & easy.

Fatal error: Call to a member function getHTMLString() on a non-object

P: 22
I am new to OO PHP and this is probably something really stupid, I have the following code in redirect.php:

[PHP]<?php
include("include_functions.php");
include("classes.xhtml.php");
include("classes.forms.php");

error_reporting(E_ALL);

ini_set('display_errors', True);

//set up XHTML Document Object
$htmlDoc = new XHTMLDocument("", "", "", "");

//Add DocType
$DocType = new DocumentType("XHTMLTransitional");
$htmlDoc->setDocumentType($DocType);

//Add Head Element
$headElement = new Head("","","","");
//Title Element
$titleElement = new Title("","","","","","","theDDHut paypal redirection");
$headElement->setTitleElement($titleElement);

//Add Script Element
$jScriptElement = new Script("text/javascript", "", "", "", "form_submission.js", "", "");
$headElement->addScriptElement($jScriptElement);

//body element
$bodyElement = new Body("","","","","","","","","","","","","","","", "","","","","","","","","","","","","");
//$bodyElement = new Body("","","","","","","","","","","","","","","", "","SubmitHiddenForm()","","","","","","","","","" ,"","");

//Add all of the pieces of the body the form is the only piece
if(isset($_POST))
{
$formElement = new Form(GetPayPalURL(),"POST","","","","","","","","" ,"","","","","","","");
foreach($_POST as $i => $value)
{
$inputField = new Input("","","","","","",$i,"","","","hidden",$valu e,"","","","","","","","");
$formElement->addElement($inputField);
}
$bodyElement->addElement($formElement);
}

$htmlDoc->setBodyElement($bodyElement);

echo($htmlDoc->getHTMLString());
?>[/PHP]

Then in classes.xhtml.php I have the following two relevant classes, but it is doing this for pretty much any class.

[PHP]
//*********************************************
// name: Documenttype
// purpose: represents a xhtml doctype object
//*********************************************
class DocumentType
{
private $m_sDocType = "";

//*********************************************
// initializer
//*********************************************
public function __construct($type)
{
switch ($type)
{
case "HTMLStrict":
$m_sDocType = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">";
break;
case "HTMLTransitional":
$m_sDocType = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">";
break;
case "HTMLFrameset":
$m_sDocType = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Frameset//EN\" \"http://www.w3.org/TR/html4/frameset.dtd\">";
break;
case "XHTMLStrict":
$m_sDocType = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">";
break;
case "XHTMLTransitional":
$m_sDocType = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";
break;
case "XHTMLFrameset":
$m_sDocType = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Frameset//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd\">";
break;
}
}

//*********************************************
// name: getHTMLString
// purpose: to output the HTML contents of the class.
// parameters: <none>
//*********************************************
public function getHTMLString()
{
return $m_sDocType;
}
}

//*********************************************
// name: XHTML Document
// purpose: represents a xhtml document object
//*********************************************
class XHTMLDocument
{
private $m_oDocType;
private $m_oHeadElement;
private $m_oBodyElement;
private $m_sProfile = "";
private $m_sDir = "";
private $m_sLang = "";
private $m_sXmlLang = "";

//*********************************************
// constructor
//*********************************************
public function __construct($profile, $dir, $lang, $xmlLang)
{
$m_sProfile = $profile;
$m_sDir = $dir;
$m_sLang = $lang;
$m_sXmlLang = $xmlLang;
}

//*********************************************
// name: setDocumentType
// purpose: to set the document type to be used in this document.
// parameters: $docType
//*********************************************
public function setDocumentType($docType)
{
$m_oDocType = $docType;
}

//*********************************************
// name: setHeadElement
// purpose: to set the head element to be used in this document.
// parameters: $head
//*********************************************
public function setHeadElement($head)
{
$m_oHeadElement = $head;
}

//*********************************************
// name: setBodyElement
// purpose: to set the body element to be used in this document.
// parameters: $body
//*********************************************
public function setBodyElement($body)
{
$m_oBodyElement = $body;
}

//*********************************************
// name: getHTMLString
// purpose: to output the HTML contents of the class.
// parameters: <none>
//*********************************************
public function getHTMLString()
{
$htmlString = "";
$htmlString = "<html>\n";
$htmlString .= $m_oDocType->getHTMLString(); //It throws the error on this line.
$htmlString .= $m_oHeadElement->getHTMLString();
$htmlString .= $m_oBodyElement->getHTMLString();
$htmlString .= "</html>\n";

return $htmlString;
}
}
[/PHP]

I get the error on this line: $htmlString .= $m_oDocType->getHTMLString();

I know this is probably something really simple, but I am just not seeing it. Any help would be greatly appreciated.
May 11 '07 #1
Share this Question
Share on Google+
27 Replies


P: 22
I don't know what happened to the reply that I responded to but Purple said to remove the m_ out of the variable names.

Thanks so much for the help, but unfortunately it didn't work, I still got the same error. I turned on debugging and it gave me a little more information:

Notice: Undefined variable: oDocType

So I am guessing I am doing something wrong in the setDocumentType function of the XHTML object.

Another question related to your suggestion, does PHP not like _ in variable names?

Thanks
May 11 '07 #2

Purple
Expert 100+
P: 404
Hi - Sorry about that - I replied and then checked again and realised I had missed the declarations at the start of the class

Still looking at it tho :)
May 11 '07 #3

Purple
Expert 100+
P: 404
Hi,

PHP is more than happy with underscore in variable names..

Purple
May 11 '07 #4

P: 22
Hi,

PHP is more than happy with underscore in variable names..

Purple
Thanks, I know it is kind of old fashioned but I like hungarian notation. :-)
May 11 '07 #5

Purple
Expert 100+
P: 404
Hi,

[PHP]public function getHTMLString()
{
$htmlString = "";
$htmlString = "<html>\n";
$htmlString .= $m_oDocType->getHTMLString(); //It throws the error on this line.
$htmlString .= $m_oHeadElement->getHTMLString();
$htmlString .= $m_oBodyElement->getHTMLString();
$htmlString .= "</html>\n";

return $htmlString;
}
} [/PHP]

The error is telling you PHP can't resolve the function you are trying to call - I am having difficulty understanding what you are expecfing the call to $m_oDocType->getHTMLString() to generate. $m_oDocType appears to hold a string value when it should be a reference to an object generated using NEW or $this (meaning this class..)

Not sure if this helps any - if you can post the missing classes I am interested enough now to run it on my system and fix it..

Purple
May 11 '07 #6

P: 22
I will post each file in a separate post, but first, I am trying to make each of the xhtml elements an object so that I can build the objects and then iterate through them and call a generic getHTMLString function on them to get the xhtml contained within it.

this will really come in handy for the head, body, div, form, etc... tags that are containers for other tags.

thanks.
May 11 '07 #7

Purple
Expert 100+
P: 404
Also

[PHP]$htmlString = "";
$htmlString = "<html>\n";[/PHP]

the first $htmlString is redundant, when setting variables $a = 'b' the variable $a is cleared and == 'b' and only 'b' :)

if it was a typo accept my apologies..

Purple
May 11 '07 #8

P: 22
include_functions.php
[PHP]
<?php
//*********************************************
// 2007 Two Miles Solutions, LLC
//
// name: include_functions.php
// programmer: jmiles
// purpose: include functions for the dd hut website
// modifications: created 05/11/2007
//*********************************************

//*********************************************
// name: GetPayPalURL
// purpose: returns the PayPal URL to redirect to
// parameters: <none>
//
// output: the URL for PayPal
//*********************************************
function GetPayPalURL()
{
$paypalURL = "https://www.paypal.com/cgi-bin/webscr"; //put the paypal address here.
return $paypalURL;
}
?>
[/PHP]
May 11 '07 #9

P: 22
it says that the code is too long, can you pm me with your email address and I will send you the files?
May 11 '07 #10

Purple
Expert 100+
P: 404
Hi

did you try :

[PHP]$doctype->getHTMLString();[/PHP]

I am not clear on where you at with scope so throw in a

[PHP]global $doctype; [/PHP]

just before it to see if it runs..

Purple
May 11 '07 #11

P: 22
Hi

did you try :

[PHP]$doctype->getHTMLString();[/PHP]

I am not clear on where you at with scope so throw in a

[PHP]global $doctype; [/PHP]

just before it to see if it runs..

Purple
I tried just throwing this in right after I set the document type on the xhtml object
[PHP]$doctype->getHTMLString();[/PHP]

and I got this notice: Notice: Undefined variable: m_sDocType

so I think you are right that it is a scope issue, but I don't know how to fix it.
May 11 '07 #12

Purple
Expert 100+
P: 404
Hi,

I think

[PHP] public function getHTMLString()
{
return $m_sDocType;
}[/PHP]

should be

[PHP]public function getHTMLString()
{
return $this->m_sDocType;
}[/PHP]
May 11 '07 #13

P: 22
Hi,

I think

[PHP] public function getHTMLString()
{
return $m_sDocType;
}[/PHP]

should be

[PHP]public function getHTMLString()
{
return $this->m_sDocType;
}[/PHP]
I think you are right, I will give it a try, but it is going to take me a while...I'm actually sitting in a training class :-)
May 11 '07 #14

Purple
Expert 100+
P: 404
I think you are right, I will give it a try, but it is going to take me a while...I'm actually sitting in a training class :-)
I hope its something interesting :)

I did pm you my hotmail email address - if you want to mail me the code zipped, I can take a look and post my findings back on this thread
May 11 '07 #15

Purple
Expert 100+
P: 404
I think class DocumentType should be somewhat more like this..

[PHP]class DocumentType
{
private $m_sDocType = "";

//*********************************************
// initializer
//*********************************************
public function __construct($type)
{
switch ($type)
{
case "HTMLStrict":
$this->m_sDocType = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">";
break;
case "HTMLTransitional":
$this->m_sDocType = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">";
break;
case "HTMLFrameset":
$this->m_sDocType = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Frameset//EN\" \"http://www.w3.org/TR/html4/frameset.dtd\">";
break;
case "XHTMLStrict":
$this->m_sDocType = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">";
break;
case "XHTMLTransitional":
$this->m_sDocType = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";
break;
case "XHTMLFrameset":
$this->m_sDocType = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Frameset//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd\">";
break;
}
}

//*********************************************
// name: getHTMLString
// purpose: to output the HTML contents of the class.
// parameters: <none>
//*********************************************
public function getHTMLString()
{
return $this->m_sDocType;
}
}[/PHP]
May 11 '07 #16

Purple
Expert 100+
P: 404
and XHTML Document like

[PHP]//*********************************************
// name: XHTML Document
// purpose: represents a xhtml document object
//*********************************************
class XHTMLDocument
{
private $m_oDocType;
private $m_oHeadElement;
private $m_oBodyElement;
private $m_sProfile = "";
private $m_sDir = "";
private $m_sLang = "";
private $m_sXmlLang = "";

//*********************************************
// constructor
//*********************************************
public function __construct($profile, $dir, $lang, $xmlLang)
{
$this->m_sProfile = $profile;
$this->m_sDir = $dir;
$m_sLang = $lang;
$m_sXmlLang = $xmlLang;
}

//*********************************************
// name: setDocumentType
// purpose: to set the document type to be used in this document.
// parameters: $docType
//*********************************************
public function setDocumentType($docType)
{
$m_oDocType = $docType;
}

//*********************************************
// name: setHeadElement
// purpose: to set the head element to be used in this document.
// parameters: $head
//*********************************************
public function setHeadElement($head)
{
$m_oHeadElement = $head;
}

//*********************************************
// name: setBodyElement
// purpose: to set the body element to be used in this document.
// parameters: $body
//*********************************************
public function setBodyElement($body)
{
$m_oBodyElement = $body;
}

//*********************************************
// name: getHTMLString
// purpose: to output the HTML contents of the class.
// parameters: <none>
//*********************************************
public function getHTMLString()
{
$htmlString = "";
$htmlString = "<html>\n";
$htmlString .= $m_oDocType->getHTMLString(); //It throws the error on this line.
$htmlString .= $m_oHeadElement->getHTMLString();
$htmlString .= $m_oBodyElement->getHTMLString();
$htmlString .= "</html>\n";

return $htmlString;
}
} [/PHP]
May 11 '07 #17

P: 22
I have made the changes, now it is crapping out on me here:
[PHP]
class XHTMLDocument
{
private $m_oDocType;
private $m_oHeadElement;
private $m_oBodyElement;
private $m_sProfile = "";
private $m_sDir = "";
private $m_sLang = "";
private $m_sXmlLang = "";

//*********************************************
// constructor
//*********************************************
public function __construct($profile, $dir, $lang, $xmlLang)
{
$this->$m_sProfile = $profile;
$this->$m_sDir = $dir;
$this->$m_sLang = $lang;
$this->$m_sXmlLang = $xmlLang;
}

[/PHP]

it is blowing up on this line
[PHP]$this->$m_sProfile = $profile;[/PHP]

it gives me this set of notifications and warnings:
Notice: Undefined variable: m_sProfile in <path> on line 77 Fatal error: Cannot access empty property in <path> on line 77

How does it not know where the variable is? I have declared it right above it.

thanks for all of your help.

FYI - one other thing I needed to do was change
[PHP]$array[] = $var[/PHP]

to
[PHP]array_push($array, $var);[/PHP]
May 11 '07 #18

Purple
Expert 100+
P: 404
Ignore the last post - I ran out of time after mis editing it :)

Try this for class XHTMLDocument..

[PHP]//*********************************************
// name: XHTML Document
// purpose: represents a xhtml document object
//*********************************************
class XHTMLDocument
{
private $m_oDocType;
private $m_oHeadElement;
private $m_oBodyElement;
private $m_sProfile = "";
private $m_sDir = "";
private $m_sLang = "";
private $m_sXmlLang = "";

//*********************************************
// constructor
//*********************************************
public function __construct($profile, $dir, $lang, $xmlLang)
{
$this->m_sProfile = $profile;
$this->m_sDir = $dir;
$this->m_sLang = $lang;
$this->m_sXmlLang = $xmlLang;
}

//*********************************************
// name: setDocumentType
// purpose: to set the document type to be used in this document.
// parameters: $docType
//*********************************************
public function setDocumentType($docType)
{
$this->m_oDocType = $docType;
}

//*********************************************
// name: setHeadElement
// purpose: to set the head element to be used in this document.
// parameters: $head
//*********************************************
public function setHeadElement($head)
{
$this->m_oHeadElement = $head;
}

//*********************************************
// name: setBodyElement
// purpose: to set the body element to be used in this document.
// parameters: $body
//*********************************************
public function setBodyElement($body)
{
$this->m_oBodyElement = $body;
}

//*********************************************
// name: getHTMLString
// purpose: to output the HTML contents of the class.
// parameters: <none>
//*********************************************
public function getHTMLString()
{
global $DocType;
$htmlString = "";
$htmlString = "<html>\n";
$htmlString .= $DocType->getHTMLString(); //It throws the error on this line.
$htmlString .= $this->m_oHeadElement->getHTMLString();
$htmlString .= $this->m_oBodyElement->getHTMLString();
$htmlString .= "</html>\n";

return $htmlString;
}
} [/PHP]

Let me know - when you get out of your training class..

Regards Purple
May 11 '07 #19

P: 22
I posted one right before your long post, did you see it? With the exception of the global declaration our code is the same now. It doesn't seem to like the $this-> reference in front of string variables.

thanks for all of your help.
May 11 '07 #20

Purple
Expert 100+
P: 404
I posted one right before your long post, did you see it? With the exception of the global declaration our code is the same now. It doesn't seem to like the $this-> reference in front of string variables.

thanks for all of your help.
I am happy to help - would be easier if you could send me the code.. :)
May 11 '07 #21

P: 22
it's on its way.
May 11 '07 #22

Purple
Expert 100+
P: 404
Hi,

I am back online - my broadband service died and has just returned - I have received the scripts and will take a look now..

Purple
May 11 '07 #23

P: 22
thanks, I really appreciate the help. I am almost done with training but will need to get working on this soon.
May 11 '07 #24

Purple
Expert 100+
P: 404
Hi,

there is rather a lot of code..

Going back to an earlier post and to provide some feedback - the change to utilising the $this-> approach to access variables defined within the class is

$this->variable_name

not

$this->$variable_name

Let me keep looking ;-)

Purple
May 11 '07 #25

P: 22
i think i have worked a lot of the major kinks out now, thanks for all of the help, you're awesome.
May 11 '07 #26

Purple
Expert 100+
P: 404
Hey - Pleased you have found your way through it..

I was worried I was fixing your college assignment until I found your website. :)

Rgds
May 11 '07 #27

P: 22
Hey - Pleased you have found your way through it..

I was worried I was fixing your college assignment until I found your website. :)

Rgds
Nope, not a college assignment, just trying to create some base classes that I can use when developing websites that make my life easier. Thanks.
May 12 '07 #28

Post your reply

Sign in to post your reply or Sign up for a free account.