0

I'm writing a class and I can't figure out why I am getting this error:

PHP Fatal error:  Call to undefined method Directory::BuildDirectoryListing() 
in C:\www\directory.php on line 25

It doesn't make any sense. By the error it looks like it is trying to look for a static function. Here is the code I am using:

$odata = new Directory($listing['id']);
$adata = $odata->BuildDirectoryListing();

<?php

include_once("database.php");

class Directory {

    public $listing = array();
    public $aacategories = array();

    function __construct($_listing) {

        $this->listing = $_listing;

    }

    public function BuildDirectoryListing() {

        /* function code here */

    }


}

?>
2
  • 4
    "Directory" is a reserved class in PHP: php.net/manual/en/reserved.classes.php Commented Aug 1, 2014 at 16:57
  • I'm not sure why that is a "-1". It seems like anyone could have the same problem. Commented Aug 1, 2014 at 17:04

2 Answers 2

2

Directory is a PHP built-in class.

You need to namespace your code or change your class name:

Class:

<?php

namespace MyApp;

class Directory {

    public $listing = array();
    public $aacategories = array();

    function __construct($_listing) {

        $this->listing = $_listing;

    }

    public function BuildDirectoryListing() {

        /* function code here */

    }

}

?>

Creating the class:

<?php

$odata = new \MyApp\Directory($listing['id']);
$adata = $odata->BuildDirectoryListing();

?>
Sign up to request clarification or add additional context in comments.

Comments

0

You are calling a static function in Directory::BuildDirectoryListing(), change this line

public function BuildDirectoryListing() {

for

public static function BuildDirectoryListing() {

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.