Jump to content

Help class function unrecognised (Call to a member function on a non-object)


phr0663r

Recommended Posts

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

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!!";

?>

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?

 

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>";

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.