Call to a member function on a non-object

Permalink
Hey!

So I'm trying my first big PHP project built from the ground up. I've been looking at a ton of controllers, models and etc so I thought I'd take a stab at it and failed....a lot. *sighs* I am calling the model from a single page. Here's the code:

Model Code:
<?php     
defined('C5_EXECUTE') or die(_("Access Denied."));
class Certification Extends Model{   
   public function getSubmitted($string_fn){
      $test = $string_fn;
      return $test;
   }
}


Single-page code
$cert = Loader::model('certification');
echo "SQL: ".$cert->getSubmitted("hello");


And I get....

Call to a member function getSubmitted() on a non-object


Appreciate any tips! Thanks!

rainmaker
View Replies: View Best Answer
JohntheFish replied on at Permalink Reply
JohntheFish
Try:
Loader::model('certification');
$cert = new Certification();
echo "SQL: ".$cert->getSubmitted("hello");


What you have done would work for a helper( where an instance is returned by Loader::helper), but for everything else, you have to create your own instance.
JohntheFish replied on at Permalink Reply
JohntheFish
PS. There are a few required patterns of c5 class and file naming conventions that may also come into play if you get more complicated.
rainmaker replied on at Permalink Reply 1 Attachment
rainmaker
Hey JohntheFish!

I tried that once too and got the following error (view attached). I'm supposed to make a table in the db?
ssrgspdkt replied on at Permalink Reply
ssrgspdkt
Try This
<?php     
defined('C5_EXECUTE') or die(_("Access Denied."));
class Certification Extends model{   
   public function getSubmitted($string_fn){
      $test = $string_fn;
      return $test;
   }
}

In Single Page like your code.
ssrgspdkt replied on at Permalink Reply
ssrgspdkt
you should place model in /models folder and filename should be in lowercase .your modelname should be certification.php .so your file structure should be /models/certification.php
rainmaker replied on at Permalink Reply
rainmaker
I got that far. It isn't throwing that kind of error. It was before anyway. ;)

Thank you thank you for your tips!
JohntheFish replied on at Permalink Best Answer Reply
JohntheFish
Looking at it again, I think the core class Model is designed to be used deep inside database stuff. The 'models' I have created do not extend the core Model, but do their own thing or inherit from Object.

So in the case of your minimal example, try:
class Certification {
...
}


Or:
class Certification extends Object {
...
}
rainmaker replied on at Permalink Reply
rainmaker
Ahhh gotcha. That makes sense! :) I'll try that angle. Thanks!