-1

私は OOP PHP を試していますが、少し問題が発生したようです。

    class Connection
{   
    public $con = false;
    public $dbSelected = false;
    public $activeConnection = null;
    public $dataBaseName = "";
    function __contruct($dbUserName, $dbPassword, $server = "localhost")
    {
        $this->con = mysql_connect($server,$dbUserName,$dbPassword);
        if(!$this->con)
        {
            $this->activeConnection = false;
        }
        else
        {
            $this->activeConnection = true;
        }
    }
    // Says the error is on the line bellow
    public function dbConnect($dbName, $identifyer = $this->con)
    {
        $this->dbSelected = mysql_select_db($dbName, $identifyer);
        $this->dataBaseName = $dbName;
        if($this->dbSelected != true)
        {
            $this->connectionErrorReport();
        }
    }
    //... Class continues on but is unimportant. 

Parse error: syntax error, unexpected T_VARIABLE in [path] on line 21 が表示されます

私はそれを長い間見つめてきたので、本当に助けが必要です。

4

8 に答える 8

4

これは有効な構文ではありません。これを行う:

public function dbConnect($dbName, $identifyer = null)
{
    if ($identifyer === null)
      $identifyer = this->con;
    //...
}
于 2012-03-13T20:39:01.720 に答える
4

問題はここにあります:

public function dbConnect($dbName, $identifyer = $this->con) { ... }

次のようになります。

public function dbConnect($dbName, $identifyer = null)
{
    $identifyer = $identifyer ? $identifyer : $this->con;
    ...
}
于 2012-03-13T20:38:22.377 に答える
3
public function dbConnect($dbName, $identifyer = $this->con)

変数を既定のパラメーターとして使用することはできません。値を設定する必要があり$identifyerます。

次のようなことができます。

public function dbConnect($dbName, $identifyer = FALSE){
    $identifyer = $identifyer === FALSE ? $this->con : $identifyer;
    // rest of function
}
于 2012-03-13T20:38:00.520 に答える
3

関数パラメーターにデフォルト値を割り当てる場合、それは定数である必要があり、実行時に割り当てられた値であってはなりません。

例えば:

public function dbConnect($dbName, $identifyer = 12345) //this is okay

public function dbConnect($dbName, $identifyer = $something) //this is not okay
于 2012-03-13T20:38:24.853 に答える
1

あなたはこれを言うことはできません: $identifyer = $this->con

デフォルト値を NULL に設定して、メソッドで確認してみてください。

于 2012-03-13T20:38:47.647 に答える
0

問題は

$identifyer = $this->con)

PHPのドキュメントには、デフォルトの引数値については...The default value must be a constant expression, not (for example) a variable, a class member or a function call.

于 2012-03-13T21:01:01.363 に答える
0

$this->con が問題ですが、パブリック関数として $this->con にアクセスできます。

 public function dbConnect($dbName )
 {
    $identifyer = $this->con;
    $this->dbSelected = mysql_select_db($dbName, $identifyer);
    $this->dataBaseName = $dbName;
    if($this->dbSelected != true)
    {
        $this->connectionErrorReport();
    }
 }
于 2012-03-13T20:39:34.390 に答える
-1

public $identifyer = $this->con単に に置き換えます$identifyer

次のように、引数のデフォルト値として静的変数のみを渡すことができますpublic function test($value = self::default_value)

これはうまくいくはずです:

public function dbConnect($dbName, $identifyer = NULL)
{
    NULL === $identifyer AND $identifyer = $this->con;
    $this->dbSelected = mysql_select_db($dbName, $identifyer);
    $this->dataBaseName = $dbName;
    if($this->dbSelected != true)
    {
        $this->connectionErrorReport();
    }
}
于 2012-03-13T20:41:35.447 に答える