"Call to undefined method" on class property!?
am 07.01.2010 09:47:52 von Allen McCabe
--001636e1fd3f6b4ec7047c8f20a7
Content-Type: text/plain; charset=ISO-8859-1
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:
$result = $db->query('SELECT * FROM `links` WHERE `category` =
\'footer_navigation\';');
$total = $db->affected_rows;
if ($total > 0)
{
$Link = new Link();
$count = $db->affected_rows($result);
$i = 0;
while ($row = $db->fetch_array($result))
{
$properties = $Link->setLinkProperties($row);
$link[$i] = '
foreach ($properties as $prop)
{
$link[$i] .= $prop;
}
$link[$i] .= '>';
$row['text'];
$link[$i] .= '';
$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:
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:
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!!
--001636e1fd3f6b4ec7047c8f20a7--
Re: "Call to undefined method" on class property!?
am 07.01.2010 10:01:11 von Darren Karstens
Do you have a method in your Database class called affected_rows() ?
From the code you have shown you only have a member variable called
affected_rows and so your call $db->fetch_array($result) is falling
over.
On Thu, Jan 7, 2010 at 8:47 AM, 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, howev=
er
> in my footer I want to loop through a properties list for links (I am sav=
ing
> links to a database, each with its own properties).
>
> Here is the code snippet that is giving me trouble:
>
> footerlinks.inc.php:
>
>
> =A0 =A0 =A0 =A0$result =3D $db->query('SELECT * FROM `links` WHERE `categ=
ory` =3D
> \'footer_navigation\';');
> =A0 =A0 =A0 =A0$total =3D $db->affected_rows;
>
> =A0 =A0 =A0 =A0if ($total > 0)
> =A0 =A0 =A0 =A0{
> =A0 =A0 =A0 =A0 =A0 =A0$Link =3D new Link();
> =A0 =A0 =A0 =A0 =A0 =A0$count =3D $db->affected_rows($result);
> =A0 =A0 =A0 =A0 =A0 =A0$i =3D 0;
> =A0 =A0 =A0 =A0 =A0 =A0while ($row =3D $db->fetch_array($result))
> =A0 =A0 =A0 =A0 =A0 =A0{
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$properties =3D $Link->setLinkProperties($=
row);
>
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$link[$i] =3D '
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0foreach ($properties as $prop)
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0{
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$link[$i] .=3D $prop;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0}
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$link[$i] .=3D '>';
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$row['text'];
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$link[$i] .=3D '';
>
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$i++;
> =A0 =A0 =A0 =A0 =A0 =A0}
>
> =A0 =A0 =A0 =A0 =A0 =A0$j =3D 0;
> =A0 =A0 =A0 =A0 =A0 =A0while ($j < $link)
> =A0 =A0 =A0 =A0 =A0 =A0{
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0echo $link[$j];
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0if ($j < $count)
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0{
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0echo ' | ';
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0}
>
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$j++;
> =A0 =A0 =A0 =A0 =A0 =A0}
>
> =A0 =A0 =A0 =A0}
>
> =A0 =A0?>
>
> The $Link->$Link->setLinkProperties() method is defined in
>
> Link.class.php:
>
>
>
> class Link {
>
> =A0 =A0public function setLinkProperties($rows)
> =A0 =A0{
> =A0 =A0 =A0 =A0if (!empty($row['href']))
> =A0 =A0 =A0 =A0{
> =A0 =A0 =A0 =A0 =A0 =A0$properties['href'] =3D 'href=3D"' . $row['href'] =
.. '" ';
> =A0 =A0 =A0 =A0}
>
> =A0 =A0 =A0 =A0if (!empty($row['class']))
> =A0 =A0 =A0 =A0{
> =A0 =A0 =A0 =A0 =A0 =A0$properties['class'] =3D 'class=3D"' . $row['class=
'] . '" ';
> =A0 =A0 =A0 =A0}
>
> =A0 =A0 =A0 =A0if (!empty($row['style']))
> =A0 =A0 =A0 =A0{
> =A0 =A0 =A0 =A0 =A0 =A0$properties['style'] =3D 'style=3D"' . $row['style=
'] . '" ';
> =A0 =A0 =A0 =A0}
>
> =A0 =A0 =A0 =A0if (!empty($row['title']))
> =A0 =A0 =A0 =A0{
> =A0 =A0 =A0 =A0 =A0 =A0$properties['title'] =3D 'title=3D"' . $row['title=
'] . '" ';
> =A0 =A0 =A0 =A0}
>
> =A0 =A0 =A0 =A0if (!empty($row['name']))
> =A0 =A0 =A0 =A0{
> =A0 =A0 =A0 =A0 =A0 =A0$properties['name'] =3D 'name=3D"' . $row['name'] =
.. '" ';
> =A0 =A0 =A0 =A0}
>
> =A0 =A0 =A0 =A0if (!empty($row['target']))
> =A0 =A0 =A0 =A0{
> =A0 =A0 =A0 =A0 =A0 =A0$properties['target'] =3D 'target=3D"' . $row['tar=
get'] . '" ';
> =A0 =A0 =A0 =A0}
>
> =A0 =A0 =A0 =A0if (!empty($row['rel']))
> =A0 =A0 =A0 =A0{
> =A0 =A0 =A0 =A0 =A0 =A0$properties['rel'] =3D 'rel=3D"' . $row['rel'] . '=
" ';
> =A0 =A0 =A0 =A0}
>
> =A0 =A0 =A0 =A0if (!empty($row['onclick']))
> =A0 =A0 =A0 =A0{
> =A0 =A0 =A0 =A0 =A0 =A0$properties['onclick'] =3D 'onclick=3D"' . $row['o=
nclick'] . '" ';
> =A0 =A0 =A0 =A0}
>
> =A0 =A0 =A0 =A0if (!empty($row['ondblclick']))
> =A0 =A0 =A0 =A0{
> =A0 =A0 =A0 =A0 =A0 =A0$properties['ondblclick'] =3D 'ondblclick=3D"' . $=
row['ondblclick']
> . '" ';
> =A0 =A0 =A0 =A0}
>
> =A0 =A0 =A0 =A0if (!empty($row['onmouseover']))
> =A0 =A0 =A0 =A0{
> =A0 =A0 =A0 =A0 =A0 =A0$properties['onmouseover'] =3D 'onmouseover=3D"' .
> $row['onmouseover'] . '" ';
> =A0 =A0 =A0 =A0}
>
> =A0 =A0 =A0 =A0if (!empty($row['onmouseout']))
> =A0 =A0 =A0 =A0{
> =A0 =A0 =A0 =A0 =A0 =A0$properties['onmouseout'] =3D 'onmouseout=3D"' . $=
row['onmouseout']
> . '" ';
> =A0 =A0 =A0 =A0}
>
> =A0 =A0 =A0 =A0return $properties;
>
> =A0 =A0} // 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:
>
>
>
> class Database {
>
> =A0 =A0private $server =A0 =3D ''; //database server
> =A0 =A0private $user =A0 =A0 =3D ''; //database login name
> =A0 =A0private $pass =A0 =A0 =3D ''; //database login password
> =A0 =A0private $database =3D ''; //database name
> =A0 =A0private $pre =A0 =A0 = ''; //table prefix
>
> =A0 =A0#######################
> =A0 =A0//internal info
> =A0 =A0private $error =3D '';
> =A0 =A0private $errno =3D 0;
>
> =A0 =A0//number of rows affected by SQL query
> =A0 =A0public $affected_rows =3D 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!!
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: "Call to undefined method" on class property!?
am 07.01.2010 12:49:16 von Lens Development
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:
>
>
> $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] = '
> foreach ($properties as $prop)
> {
> $link[$i] .= $prop;
> }
> $link[$i] .= '>';
> $row['text'];
> $link[$i] .= '';
>
> $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:
>
>
>
> 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:
>
>
>
> 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!!
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php