Re: require/include using a class ...

From: Date: Tue, 25 Jul 2000 13:30:21 +0000
Subject: Re: require/include using a class ...
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-8036@lists.php.net to get a copy of this message
Mark Walter:
Hi, my problem is as follows. My header and footer is included with 'require' and it works right well: <? require(footer); ?> It's html code in opposite to 'class_mysql.php'. I tried to included them with this example: <? require(class_mysql.php); ?>
<? include('class_mysql.php'); ?> Will also work. Technically, you want to use include() in conjunction with a conditional, and require() when it needs to be included regardless of the circumstances, but I have stopped using require() altogether. Maybe someone else will clarify. (more below)
The right's should be ok: luxer@server:~/bremer > ls -la class_mysql.php
-rwxr-xr-x   1 luxer    users         735 Jul 25 13:15 class_mysql.php
Here is the code to be included within the HTML site: <?
       class mysql
{
var     $SERVER =       "localhost";
var     $USER   =       "root";
var     $PASS   =       "";
var     $DBASE  =       "INTERSWAP";
       function error($text)
{
       $no     =       mysql_errno();
       $msg    =       mysql_error();
       echo "[$text] ( $no : $msg ) <br> \n";
       exit;
}
       function init ()
{
I'm not sure what else you're going to include in this code, but you may want to make init() your constructor. In other words, give that method the same name as your class. When you create a new instance of that class (see below), your constructor (the function (method) that has the same name as the class itself) will execute automatically.
$server         =       $this->SERVER;
$user           =       $this->USER;
$pass           =       $this->PASS;
$dbase          =       $this->DBASE;
$conn   =       mysql_connect($server, $user, $pass);
if(!$conn) {
       $this->error("no connection ...");
} if($mysql_select_db($dbase,$conn) {
       $this->error("no database ...");
}
       $this-> CONN = $conn;
       return true;
} } ?> This is my first day creating objects with php so maybe the problem is sitting 60 cm in front of the code. ;-) I haven't tried to call the functions. My browser won't show me the page after included the class either with require nor with include. So that should be a unregularity as the html code won't be displayed on my screen.
Remember that simply including or requiring a class in the header of your document won't automatically let you access its methods. You have to create a new object/instance/dohickey Something along the lines of: <?php include('class_mysql.php'); $object = new mysql; $object->init(); ?>
Any help will be appreciated. Thank's in advance.
Brian

« previous php.general (#8036) next »