Re: include/require not allowed in classes?

From: Date: Fri, 14 Nov 2003 14:02:02 +0000
Subject: Re: include/require not allowed in classes?
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-169635@lists.php.net to get a copy of this message
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 --

« previous php.general (#169635) next »