Re: "Call to undefined method" on class property!?

From: Date: Thu, 07 Jan 2010 11:49:16 +0000
Subject: Re: "Call to undefined method" on class property!?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-300878@lists.php.net to get a copy of this message
Allen McCabe wrote:
I have a singleton database object in a global file at the top of my document. In most other locations I am able to access it just fine, however in my footer I want to loop through a properties list for links (I am saving links to a database, each with its own properties). Here is the code snippet that is giving me trouble: footerlinks.inc.php: <?php
        $result = $db->query('SELECT * FROM links WHERE category =
\'footer_navigation\';');
        $total = $db->affected_rows;
Here you use affected_rows as a class property to assign a value to a variable.
        if ($total > 0)
        {
            $Link = new Link();
            $count = $db->affected_rows($result);
Here you call affected_rows as a class method. And from looking at your Database class. affected_rows is a property not a method. So that explains the undefined method error.
            $i = 0;
            while ($row = $db->fetch_array($result))
            {
                $properties = $Link->setLinkProperties($row);
                $link[$i] = '<a ';
                foreach ($properties as $prop)
                {
                    $link[$i] .= $prop;
                }
                $link[$i] .= '>';
                $row['text'];
                $link[$i] .= '</a>';
                $i++;
            }
            $j = 0;
            while ($j < $link)
            {
                echo $link[$j];
                if ($j < $count)
                {
                    echo ' | ';
                }
                $j++;
            }
        }
    ?>
The $Link->$Link->setLinkProperties() method is defined in Link.class.php: <?php class Link {
    public function setLinkProperties($rows)
    {
        if (!empty($row['href']))
        {
            $properties['href'] = 'href="' . $row['href'] . '" ';
        }
        if (!empty($row['class']))
        {
            $properties['class'] = 'class="' . $row['class'] . '" ';
        }
        if (!empty($row['style']))
        {
            $properties['style'] = 'style="' . $row['style'] . '" ';
        }
        if (!empty($row['title']))
        {
            $properties['title'] = 'title="' . $row['title'] . '" ';
        }
        if (!empty($row['name']))
        {
            $properties['name'] = 'name="' . $row['name'] . '" ';
        }
        if (!empty($row['target']))
        {
            $properties['target'] = 'target="' . $row['target'] . '" ';
        }
        if (!empty($row['rel']))
        {
            $properties['rel'] = 'rel="' . $row['rel'] . '" ';
        }
        if (!empty($row['onclick']))
        {
            $properties['onclick'] = 'onclick="' . $row['onclick'] . '" ';
        }
        if (!empty($row['ondblclick']))
        {
            $properties['ondblclick'] = 'ondblclick="' . $row['ondblclick']
. '" ';
        }
        if (!empty($row['onmouseover']))
        {
            $properties['onmouseover'] = 'onmouseover="' .
$row['onmouseover'] . '" ';
        }
        if (!empty($row['onmouseout']))
        {
            $properties['onmouseout'] = 'onmouseout="' . $row['onmouseout']
. '" ';
        }
        return $properties;
    } // END OF METHOD setLinkProperties
} // END OF CLASS ?> Also for reference, the method query() in my database class sets a value for $affected_rows: Database.class.php: <?php class Database {
    private $server   = ''; //database server
    private $user     = ''; //database login name
    private $pass     = ''; //database login password
    private $database = ''; //database name
    private $pre      = ''; //table prefix
    #######################
    //internal info
    private $error = '';
    private $errno = 0;
    //number of rows affected by SQL query
    public $affected_rows = 0;
... ?> This is the error I am receiving: "*Fatal error*: Call to undefined method Database::affected_rows() in * /home/mwclans1/public_html/components/footerlinks.inc.php* on line *9*" Please help PHP gurus!!


« previous php.general (#300878) next »