Jump to content

Call to class error undefined $obj


son.of.the.morning

Recommended Posts

Alright guys got a but of a issue here, i keep geting a 'Call to class error undefined $obj' and i cant seem to see the problem.

 

my class

<?php

class DatabaseInsert {

	function ArticleInsert($table,$fields,$values) {

		$values_imploded = implode("','",$values);
		$fields_imploded = implode(",",$fields);
		$i = "INSERT INTO $table ($fields_imploded) VALUES ($values_imploded)";

		mysql_query($i) or die( "<br>Query string: <br>Produced error: " . mysql_error() );;
		//$table,$fields,$values

	}
}



?>

 

The call

if(isset($_POST['sub'])) {

					include("../scrips/php/cms/database.insert.class.php");
					$table = "blog_posts";
					$title = "'".$_POST['ArticleTitle']."'";
					$img = "'2'";
					$post = "'".$_POST['ArticleBody']."'";
					$aurthor_id = "'1'";
					$category_id = "'4'";
					$date_posted = "NOW()";

					$values = array("$title","$img","$post","$aurthor_id","$category_id","$date_posted");
					$fields = array('title','img','post','aurthor_id','category_id','date_posted'); 
					//echo $values['0']."<br/>".$fields['0'];

					echo $values['0']."<br/>".$values['1']."<br/>".$values['2']."<br/>".$values['3']."<br/>".$values['4']."<br/>".$values['5'];
					$obj->ArticleInsert($table,$fields,$values);

Link to comment
https://forums.phpfreaks.com/topic/252302-call-to-class-error-undefined-obj/
Share on other sites

You can't invoke a method unless one of two things happens:

 

1. You instantiate a new object of the type you want to use, i.e.:

 

$obj = new DatabaseInsert();
$obj -> ArticleInsert($table, $fields, $values);

 

2. You declare the method as static:

 

class DatabaseInsert
{
   public static function ArticleInsert($table, $fields, $values)
   {
      // blah
   }
}

// ...

DatabaseInsert::ArticleInsert($table, $fields, $values);

 

That said, what you're attempting to do isn't very OOP.  You're not gaining anything by simply stuffing your function in a class.  Grouping functions by theme is not OOP, regardless of whether or not you use classes and objects.

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.