-1

Class to display different views for my blog.

class SB_Display { public function __contruct() { include_once('settings/db.settings.php'); $mysqli = new mysqli($SB_dbsettings['host'],$SB_dbsettings['user'],$SB_dbsettings['pass'],$SB_dbsettings['dbname']); } private function List_Display() { $VIEW = ''; include_once('views/list.html.view.php'); $sql = "SELECT * FROM sb_posts ORDER BY ID DESC LIMIT '$SETTINGS->maxposts'"; $sql = $mysqli->real_escape_string($sql); $res = $mysqli->mysqli_query($sql); if($res->numrows > 0) { $res->data_seek(0); while ($row = $res->fetch_assoc()) { foreach($row as $key => $value) { $BLOG->$key = $value; $VIEW .= $HTML; } } } else { $VIEW .= 'No Posts To Display'; } return $VIEW; } private function Single_Display($id) { $VIEW = ''; include_once('views/single.html.view.php'); $sql = "SELECT * FROM sb_posts WHERE BID = '$id'"; $sql = $mysqli->real_escape_string($sql); $res = $mysqli->mysqli_query($sql); $row = $res->fetch_assoc(); foreach($row as $key => $value) { $BLOG->$key = $value; } $VIEW .= $HTML; return $VIEW; } private function Create_Display() { include_once('views/create.html.view.php'); return $HTML; } private function Edit_Display($id) { $VIEW = ''; $sql = "SELECT * FROM sb_posts WHERE BID = '$id'"; $sql = $mysqli->real_escape_string($sql); $res = $mysqli->mysqli_query($sql); $row = $res->fetch_assoc(); foreach($row as $key => $value) { $BLOG->$key = $value; } $BLOG->id = $id; $VIEW .= $HTML; return $VIEW; } public function SB_Get_Display($type,$id) { switch($type) { case 'list': $this->content = List_Display(); return $this; break; case 'single': $this->content = Single_Display($id); return $this; break; case 'create': $this->content = Create_Display(); return $this; break; case 'edit': $this->content = Edit_display($id); return $this; break; } } } 

When using this class in the following manner ..

$BODY = new SB_Display(); $BODY->SB_Get_Display('list',''); 

I get this error:

Fatal error: Call to undefined function List_Display()

I can't figure out why. Any help would be greatly appreciated.

3
  • 1
    Because of the include_once when creating the db connection, you'll never be able to create a valid second instance of this class. You might should reconsider the structure of this. Typically an include or include_once inside of a method is a very bad idea. (And your mysqli is only being saved in a local variable, not on the object.) Commented May 31, 2012 at 21:40
  • possible duplicate of Call a function inside class Commented May 31, 2012 at 21:41
  • @Corbin Thanks for the advice noted and taken on board. :) Commented May 31, 2012 at 22:00

2 Answers 2

4

You need to use $this->function() instead of function() to call a method.

On a side-note, your constructor function name is incorrect. It's __contruct() but needs to be __construct() to be used as a constructor. Besides that, your indentation is horrible and makes the code hard to read.

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

2 Comments

first apologies for untidy indentation, and thanks for spotting my constructor mistake. Where do I need to use $this->function() , in the switch ? where i am currently using $this->content = function() ?
You always need to use $this-> when you want to access a member of the class - be it a variable or a method. So instead of $this->content = List_Display(); use $this->content = $this->List_Display();
0

I agree with @Corbin, that's a very bad idea. Also, personally, I love the autoload classes approach.

Some might say it's sloppy and the easy way out, but it forces you to really think about your classnames and directories, plus you avoid problems when you're renaming files/ classes. In your case you'd have to search for all files trying to include it and rename everything manually.

"my" approach: create a inc.php or something in the root of your site, and put this in it:

PHP

//autoload classes function __autoload($class_name){ set_include_path(get_include_path().PATH_SEPARATOR.'/usr/share/file/'); //also //include magic file location // put the path to your class files here $path = $_SERVER['DOCUMENT_ROOT'].'/lib/classes/'; // tell PHP to scan the default include path AND your include path set_include_path(get_include_path() . PATH_SEPARATOR . $path); // name your classes and filenames with underscores, i.e., Net_Whois stored in //Net_Whois.php $classfile = str_replace("_", DIRECTORY_SEPARATOR, $class_name) . ".php"; require_once($classfile); } 

just initialise your db connections in the inc.php file as well and you can access them pretty much anywhere.

now just include the inc.php file in every new class you create, and you never have to look back again( depending on the project ofcourse)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.