Welcome Guest, Not a member yet? Register   Sign In
Fatal error: Call to a member function on a non-object
#1

[eluser]neverstop[/eluser]
Hello i'm developing a site and choose CI to do it. I'm doing just fine and solved all problems i had except this one. I'll post the error:

Quote:A PHP Error was encountered
Severity: Notice
Message: Undefined property: Main::$Artigos
Filename: controllers/main.php
Line Number: 13

Fatal error: Call to a member function showArticles() on a non-object in C:\Wamp\www\system\application\controllers\main.php on line 13

My main.php is:
Code:
<?php
class Main extends Controller
{
    function Main()
    {
        parent::Controller();
    }
    
    function index()
    {        
        $this->load->library('Artigos');
        $val["title"] = "Altus :: Evoluçao em Automaçao";
        $val["articles"] = $this->Artigos->showArticles();
        $this->load->view('mainview', $val);
    }
}
?>
As you can see i created a new library and put it in the /libraries folder. Well it is working fine, at least CI load it with success ...and i'm sure about this because when i change its name i receive an error at the screen as expected.

My library file is:
Code:
<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');
    
class Artigos
{
    function showArticles()
    {        
        $query = $this->db->query("SELECT * FROM artigos");
        $articles = "";
        foreach ($query->result() as $row)
        {
            $articles .= "<p><b>";
            $articles .= $row->date . "&nbsp;&middot;&nbsp;";
            $articles .= "<a href=\"#\">" . $row->name . "</a></b><br>";
            
            $content_complete = $row->content;
            $content_edited = html_entity_decode(substr($content_complete, 0, 120));
            $last_position = strrpos($content_edited, " ");
            
            $articles .= substr($content_edited, 0, $last_position) . "&nbsp;...</p>";
        }
        return($articles);
    }
    
    function _paginationArticles($num_rows)
    {
        $this->load->library("pagination");
        $config["full_tag_open"] = "<div id=\"article_pagination\">";
        $config["full_tag_close"] = "</div>";
        $config["total_rows"] = $num_rows;
        $config["first_link"] = "Inicial";
        $config['last_link'] = "Final";
        $config["per_page"] = 10;
        $config["num_links"] = 9;
        $this->pagination->initialize($config);
        return($this->pagination->create_links());
        
        //$pagination = $this->_paginationArticles($query->num_rows());
    }
}
?&gt;
I need to configure something on CI? I read the documentation to create my own libraries and everything seems to be correct. Help me please, i don't know what to do now =/
#2

[eluser]abmcr[/eluser]
into your library don't use
$this->db->query....

but

$CI =& get_instance();
$CI->db->query

see in the help file

Ciao!
#3

[eluser]neverstop[/eluser]
Hi and tanks for the answer. Yes you are right in using $CI =& get_instance(); but still not working. I changed my code to a simple one, take a look:
Code:
&lt;?php
class Main extends Controller
{
    function __construct()
    {
        parent::Controller();
    }
    
    function index()
    {        
        $this->load->library("Artigos");
        $val["title"] = "Altus :: Evoluçao em Automaçao";
        $this->Artigos->showArticles();
        $this->load->view("mainview", $val);
    }
}
?&gt;
and
Code:
&lt;?php
if (!defined("BASEPATH"))
    exit('No direct script access allowed');
    
class Artigos
{
    function showArticles()
    {    
        /*
        $CI =& get_instance();
        $query = $CI->db->query("SELECT * FROM artigos");
        $articles = "";
        foreach ($query->result() as $row)
        {
            $articles .= "<p><b>";
            $articles .= $row->date . "&nbsp;&middot;&nbsp;";
            $articles .= "<a href=\"#\">" . $row->name . "</a></b><br>";
            
            $content_complete = $row->content;
            $content_edited = html_entity_decode(substr($content_complete, 0, 120));
            $last_position = strrpos($content_edited, " ");
            
            $articles .= substr($content_edited, 0, $last_position) . "&nbsp;...</p>";
        }
        return($articles . $this->paginationArticles());
        */
    }
}
?&gt;
As you can see i commented the code of my function. And now whats the problem? Lol seems to be very simple but i can't get the solution to my problem =/. The same erros still ocurring.
#4

[eluser]abmcr[/eluser]
Are you sure the name of the file is correct?
#5

[eluser]eggshape[/eluser]
Hey Neverstop:

Your error is that you don't have a contructor for your class Artigos. You need to define it using

Code:
function Artigos(){
//nothing needs to go here unless you want it to be automatically called when Artigos is instantiated
}

//or

function __construct(){
//same here
}

when you write $this->Artigos->do_something() , you're telling CI there is an instantiated object with that name that has a method called do_somehing.

If you want to access methods from Artigos without instantiating it as an object, you need to use 'static public function' to define your methods and then use it like this 'Artigos::do_something()', after including the file.

You can read more at php.net
#6

[eluser]neverstop[/eluser]
Now i'm not understanding anything =/.

Doesn't CI create its own constructor when i instanciate a class without one? As java does? O read the documentation of how create my own library and there's no constructor on the examples.

Well i still fighting here to understand the CI and changed my code and it worked! Take a look:
Code:
&lt;?php
class Main extends Controller
{
    function __construct()
    {
        parent::Controller();
    }
    
    function index()
    {        
        $this->load->library("Artigos");
        $val["title"] = "Altus :: Evoluçao em Automaçao";
        $val["articles"] = Artigos::showArticles();
        $this->load->view("mainview", $val);
    }
}
?&gt;
and
Code:
if (!defined("BASEPATH"))
    exit('No direct script access allowed');
    
class Artigos
{
    function showArticles()
    {    
        $query = $this->db->query("SELECT * FROM artigos");
        $articles = "";
        foreach ($query->result() as $row)
        {
            $articles .= "<p><b>";
            $articles .= $row->date . "&nbsp;&middot;&nbsp;";
            $articles .= "<a href=\"#\">" . $row->name . "</a></b><br>";
            
            $content_complete = $row->content;
            $content_edited = html_entity_decode(substr($content_complete, 0, 120));
            $last_position = strrpos($content_edited, " ");
            
            $articles .= substr($content_edited, 0, $last_position) . "&nbsp;...</p>";
        }
        return($articles);
    }
Now my BIG question, my class Artigos doesn't need to extend another class right? I dont need to create a constructor to my class but i could if necessary right?
In my code above i called the funcion showArticles() as a static function ok? I know it worked but i'm confuse...because the documentation explain in a different way =/
#7

[eluser]eggshape[/eluser]
I am confused now too. I thought PHP behaved differently. What I said before doesn't even make sense to me. I agree that you are calling it as a static function even without the keyword in the definition, so I take it in PHP4 these things are very loose. I guess it's obviously you don't need a constructor unless you want to do something when you create the object, but why didn't it work before? Ha. %(

Because it only works using the :: , I have to conclude that Artigos hasn't been instantiated properly. Is that right?
#8

[eluser]tonanbarbarian[/eluser]
the issue is very simple
when you load a library it is created in the controller lowercase

try this it should work
Code:
&lt;?php
class Main extends Controller
{
    function __construct()
    {
        parent::Controller();
    }
    
    function index()
    {        
        $this->load->library("Artigos");
        $val["title"] = "Altus :: Evoluçao em Automaçao";
        $this->artigos->showArticles(); // note lowercase
        $this->load->view("mainview", $val);
    }
}
#9

[eluser]eggshape[/eluser]
hahaha. thanks tonanbarbarian. =)
#10

[eluser]neverstop[/eluser]
kkk lol thats funny...now its working thanks guys!
Do not forget that you should use $CI =& get_instance(); inside your library! Well thats it, really thanks Smile




Theme © iAndrew 2016 - Forum software by © MyBB