You are here: Re: New To PHP - Error In My Class « PHP Programming Language « IT news, forums, messages
Re: New To PHP - Error In My Class

Posted by B.r.K.o.N.j.A on 08/11/06 14:45

cyber0ne wrote:
> Using PHP 5.1.2
>
> I'm working on a personal project to get used to PHP, and I seem to be
> stuck at the moment on a class I'm writing. Here is the code for the
> class, with a comment highlighting where it dies when a function is
> called externally from the code that instantiates the class:
>
> ----------BEGIN PASTE----------
> class DataAccess{
>
> private $databaseConnection;
>
> function __construct()
> {
> @ $databaseConnection = new mysqli("this", "is", "fake", "data");
> if (mysqli_connect_errno())
> {
> // TODO: generate an error
> }
> }
>
> function __destruct()
> {
> $databaseConnection->close();
> }
>
> function GetArticlesForCategory($cid)
> {
> $querySelect = "SELECT stuff WHERE something = " . $cid;
>
> // Testing shows that the following line is where the code dies.
> $resultSet = $databaseConnection->query($querySelect);
> // End dying code
>
> if ($resultSet)
> {
> $rowCount = $resultSet->num_rows;
> if ($rowCount > 0)
> {
> $i = 0;
> while ($i < $rowCount)
> {
> $rows[$i] = $resultSet->fetch_assoc();
> $i++;
> }
> $resultSet->free();
> return $rows;
> }
> else
> {
> // TODO: report an empty set
> $resultSet->free();
> return;
> }
> }
> else
> {
> // TODO: generate an error
> $resultSet->free();
> return;
> }
> }
>
> }
> ------------END PASTE------------
>
> The error log seems to be claiming that $databaseConnection is
> undefined on that line of code. Is there maybe something syntactic
> that I'm missing? I've tried replacing all 3 references to
> $databaseConnection in the class functions with
> $this->$databaseConnection, but that didn't seem to change anything.
>
> Any ideas?
>

the way you did it in constructor the variable databaseConnection has
scope only in that particular method, you should use

@ $this->databaseConnection = new mysqli("this", "is", "fake", "data");

and allways reference that property via $this->databaseConnecion...

you were using local variables where you really needed class properties


--

B.r.K.o.N.j.A = Bionic Robotic Knight Optimized for Nocturnal Judo and
Assasination

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация