Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Call to a member function get_public_name() on a non-object
  • I apologize in advance for what should be a pretty simple issue. I am very new to fuel. I recently acquired an existing site created in fuel 1.0. I have been tasked with moving this site from its existing server to a new server. The issue I am facing now is when I try to load the page I get the call to a member function error. The error is coming from the index.php view. 
    [code]
    <div class="<? if(!$contractor->logo){ ?>span12<? } else { ?>span8<? } ?>">
                    <span id="contractor_recommended" class="alert alert-success">
                            <p>
                                <span style="font-size: .7em;">
                                    <?=\Config::get('site.service_name')?> is recommended by:<br />
                                </span>
                                <strong>
                                    <? if($contractor->website_url <> ""){ ?>
                                        <a href="<?=\Helper::format_web_address($contractor->website_url);?>" target="_blank">
                                    <? } ?>
                                    <?=$contractor->get_public_name();?>
                                    <? if($contractor->website_url <> ""){ ?>
                                        </a>
                                    <? } ?>
                                </strong>
                            </p>
                    </span>
                </div>
    [/code]

    I know I am missing something, but I just can't seem to see what it is. I also know the site needs updated to current fuel release, but my client wants to get the site transferred and running before we begin that phase.

    Any help you could give would be greatly appreciated.
  • Here is the model code: 

    [code]
    namespace Contractor
    class Model_Contractor extends \Orm\Model {
        const CODE_PREFIX = 'C';
        private $_customer = null;

    public function get_public_name($default_if_blank = "") {
                if($this->public_name <> "") 
                {
                    return $this->public_name;
                } 
                else 
                {
                    if(isset($this->customer_id) and !empty($this->customer_id) and $this->get_customer()->name <> "") 
                    {
                        return $this->get_customer()->name;
                    } 
                    else 
                    {
                        return $default_if_blank;
                    }
                }
    }
    [/code]
  • Not really Fuel related, it's a normal PHP error.

    You will get this error when you call a method on a variable that is not an object.

    On which line of the view exactly do you get this error? If $contractor is not an object, the first line in the view referencing this variable would already trigger a notice error, unless error reporting is disabled (which is a bad thing if you're developing or debugging).

    Your first port of call would be to look into the controller that uses this view, check which variable is passed as the view variable "contractor", and var_dump() it so see what it contains. And from there track back to see why it doesn't contain the expected object.

    My assumption is because of a database look-up that doesn't give a result, and the result is not checked.
  • I get the error on "<?=$contractor->get_public_name();?>". 

    What I don't understand is, this site currently works on the old server. It didn't fail until I copied the code over. As of right now, the only thing I have changed is the config files to point to the new servers database. Another thing I have noticed is if I try to go to a different page, such as /login the code works but the css is not there.
  • Fact remains, the error is caused by $contractor not being an object. It's difficult to judge why that is the case. Perhaps the databases are not identical?

    What OS and webserver is running on the old server? And which PHP version? And how about the new server? The css error smells like a rewriting issue...
  • Both servers are running linux, new is ubuntu server 14.04 the other is redhat. Both are running the same version of php. 

    I checked my log file and noticed that I am getting this same error on another call as well. Not sure what has changed to make these no longer objects but I am getting close to figuring it out.

    I fixed the css issue. The project was using a package called casset. This package was trying to write to a cache directory in the assests directory. This did not exist on the new server. The directory was in the gitignore so I did not have it when I moved everything over.

    Thank you for your help so far.
  • I'm afraid you will have to start debugging to figure out why the variable passed from the controller to the view is not an object.

    The error should give you a backtrace so it's easy to find which controller it is. Assuming it is programmed a bit decent, debugging should be less than 10 minutes work...
  • The key phrase is "programmed a bit decent". I do not believe this one was. It doesn't seem to follow what I have read in the docs. The backtrace points to corepath/bootstrap.php. So I will look there.
  • Actually, I just found out that the old server is running php 5.4 and the new one is running 5.5.9.
  • I believe I have found the problem. For some reason, the if statements in the view are being commented out when the code is ran on the server. No idea why, never seen this before.
  • Are they using short notation by any chance? So

    <? if (true): ?>
    ...
    <? endif; >

    instead of

    <?php if (true): ?>
    ...
    <? endif; >

    Never seen PHP code comment itself out, that is a new one... ;)

    Hope you will manage to sort it.
  • That was the problem. php short_tags where turned off. Once I turned them on it started to work.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion