son.of.the.morning Posted December 2, 2011 Share Posted December 2, 2011 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); Quote Link to comment https://forums.phpfreaks.com/topic/252302-call-to-class-error-undefined-obj/ Share on other sites More sharing options...
KevinM1 Posted December 2, 2011 Share Posted December 2, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/252302-call-to-class-error-undefined-obj/#findComment-1293437 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.