phr0663r Posted February 14, 2008 Share Posted February 14, 2008 Ok, let me just show you the code! first the class: in file phpPage.class.php <?php class phpHeader{ private $contains; function __construct(){ $contains="<head><title>mytemplate</title></head>"; } function output(){ echo"$contains"; } function addToContains($text){ $contains.="$text"; } } class phpBody{/*see header same as!*/} class phpPage{ private $header; private $body; function output(){ $header->output(); // just echo's contains $body->output(); // just echo's contains } // initialises page to default settings function __construct(){ $header = new phpHeader; $body = new phpBody; } function addTextToBody($textToAdd){ $body->addline($textToAdd); } } ?> now the code that calls it: file index.php <?php require_once('phpPage.class.php'); if( $myPage= new phpPage ) echo"my page has been made<br>"; if($myPage->output()) echo"output succesfull"; else echo"bloody, bloody and bloody!!"; ?> always breaks on line $header->output() in phpPage::output() phpPage.class.php with error: Fatal error: Call to a member function output() on a non-object in C:\wamp\www\phpPage.class.php on line 80 but i know it is having init then in the __construc func any ideas? what gives ??? it works with myPage->output() in index.php Link to comment https://forums.phpfreaks.com/topic/91092-help-class-function-unrecognised-call-to-a-member-function-on-a-non-object/ Share on other sites More sharing options...
rhodesa Posted February 14, 2008 Share Posted February 14, 2008 To access variables within a class, you need to use $this->variablename like so: <?php class phpHeader{ private $contains; function __construct(){ $this->contains="<head><title>mytemplate</title></head>"; } function output(){ echo $this->contains; } function addToContains($text){ $this->contains .= $text; } } class phpBody{/*see header same as!*/} class phpPage{ private $header; private $body; function output(){ $this->header->output(); // just echo's contains $this->body->output(); // just echo's contains } // initialises page to default settings function __construct(){ $this->header = new phpHeader; $this->body = new phpBody; } function addTextToBody($textToAdd){ $this->body->addline($textToAdd); } } ?> <?php if( $myPage= new phpPage ) echo"my page has been made<br>"; if($myPage->output()) echo"output succesfull"; else echo"bloody, bloody and bloody!!"; ?> Link to comment https://forums.phpfreaks.com/topic/91092-help-class-function-unrecognised-call-to-a-member-function-on-a-non-object/#findComment-466855 Share on other sites More sharing options...
phr0663r Posted February 14, 2008 Author Share Posted February 14, 2008 thank you!!!!! i am a silly ......lol thanks! Link to comment https://forums.phpfreaks.com/topic/91092-help-class-function-unrecognised-call-to-a-member-function-on-a-non-object/#findComment-466865 Share on other sites More sharing options...
phr0663r Posted February 14, 2008 Author Share Posted February 14, 2008 at the risk of making my bad rep even worse! i added the $this-> in for all member variable access but it just keeps throwing Fatal error: Cannot access empty property in C:\wamp\www\phpPage.class.php I thaught at first (most such calls come from within __constrct() funcs that they didn't exist yet! but no, because it does it to all of them! any ideas? Link to comment https://forums.phpfreaks.com/topic/91092-help-class-function-unrecognised-call-to-a-member-function-on-a-non-object/#findComment-466869 Share on other sites More sharing options...
trq Posted February 14, 2008 Share Posted February 14, 2008 Can we see your current code? Link to comment https://forums.phpfreaks.com/topic/91092-help-class-function-unrecognised-call-to-a-member-function-on-a-non-object/#findComment-466871 Share on other sites More sharing options...
phr0663r Posted February 14, 2008 Author Share Posted February 14, 2008 yeah sure! phpPage.class <?php class phpHeader{ private $contains; function __construct(){ $this->$contains="<head><title>mytemplate</title></head>"; } function output(){ echo "this->$contains"; } function addToContains($text){ $contains.="$text"; } } class phpBody{ private $contains; function __construct(){ $this->$contains="<body><p>this is a body</p></body>"; } function output(){ echo "this->$contains"; } function addToContains($text){ $contains.="$text"; } } class phpPage{ private $header; private $body; function output(){ $this->$header->output(); // just echo's contains $this->$body->output(); // just echo's contains } // initialises page to default settings function __construct(){ $this->$header = new phpHeader; $this->$body = new phpBody; } function addTextToBody($textToAdd){ $this->$body->addline($textToAdd); } } ?> and the index.php <?php require_once('phpPage.class.php'); if( $myPage= new phpPage ) echo"my page has been made<br>"; if($myPage->output()) echo"output succesfull"; else echo"bloody, bloody and bloody!!"; ?> error stands as Fatal error: Cannot access empty property in C:\wamp\www\testpage.class.php on line 5 line 5 = $this->$contains="<head><title>mytemplate</title></head>"; Link to comment https://forums.phpfreaks.com/topic/91092-help-class-function-unrecognised-call-to-a-member-function-on-a-non-object/#findComment-466900 Share on other sites More sharing options...
Daniel0 Posted February 14, 2008 Share Posted February 14, 2008 It must be $this->contains. Link to comment https://forums.phpfreaks.com/topic/91092-help-class-function-unrecognised-call-to-a-member-function-on-a-non-object/#findComment-466942 Share on other sites More sharing options...
rhodesa Posted February 14, 2008 Share Posted February 14, 2008 Also, if you are just echoing the contents of a variable, don't wrap it in quotes. So use just $text instead of "$text" Link to comment https://forums.phpfreaks.com/topic/91092-help-class-function-unrecognised-call-to-a-member-function-on-a-non-object/#findComment-466947 Share on other sites More sharing options...
phr0663r Posted February 14, 2008 Author Share Posted February 14, 2008 when i use echo $this->$contains; i get the fllowing error Parse error: syntax error, unexpected T_OBJECT_OPERATOR, expecting ',' or ';' in C:\wamp\www\testpage.class.php on line 8 thats why i put it back between "'s could that be something to do with it? Link to comment https://forums.phpfreaks.com/topic/91092-help-class-function-unrecognised-call-to-a-member-function-on-a-non-object/#findComment-466960 Share on other sites More sharing options...
phr0663r Posted February 14, 2008 Author Share Posted February 14, 2008 sorry! i'll answer that! phr0663r your a !*^!$^!*!!!! you where referring to the other lines!!!! like $this->$contains.=$text; soorryyyyyy!! Link to comment https://forums.phpfreaks.com/topic/91092-help-class-function-unrecognised-call-to-a-member-function-on-a-non-object/#findComment-466967 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.