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

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.