2016-03-03 97 views
0

我已經習慣在這樣的PHP函數來設置,並宣佈我的參數調用一個函數聲明一個變量:如何定義,並通過在PHP類

function foo($length = 24) { ...

現在我想這樣做與一類功能:通過調用

class foo{ 
    function configuration() { 
     $this->defaultLength = 24; 
    } 
    function foo() { 
     $this->configuration(); 
    } 
    function test($length = $this->defaultLength) {... 

$r = new foo(); 
$r->test(); 

我收到此錯誤信息: 解析ERR或:語法錯誤,是/ www/HTDO ..意外「$這一」(T_VARIABLE)

我如何定義和一個PHP類中聲明一個變量?

回答

1

你不能在你的函數簽名參考$this。你可以這樣做:

public function test ($length = null) { 
    if ($length === null) { 
     $length = $this->defaultLength; 
    } 
} 
+0

他爲什麼不能?如果他想用它作爲'$ r-> test();'然後'$ this-> defaultLength'是可訪問的,如果他聲明它在同一個範圍內(類內)。 – KDOT

+1

在功能裏面,是的。但不在函數簽名中,因爲函數簽名是在任何實例創建之前讀取的。 – Erik

2

我這樣做:

class foo 
{ 
    const DEFAULT_LENGHT = 24; 

    public function test($length = self::DEFAULT_LENGHT) 
    { 
     var_dump($length);  
    } 
} 

$foo = new foo(); 
$foo->test(); // // int(24) 

如果您需要更改運行時的值,你可以做這樣的事情:

class foo 
{ 
    protected $default_lenght = 24; 

    public function setDefaultLenght($lenght) 
    { 
     $this->default_lenght = $lenght; 
    } 

    public function test($length = null) 
    { 
     $length = $length ?: $this->default_lenght; 
     var_dump($length); 
    } 
} 

$foo = new foo(); 
$foo->test(); // int(24) 
$foo->setDefaultLenght(42); 
$foo->test(); // int(42) 
+0

我會讓$ default_lenght保護,因爲你確實有一個setter! –

+0

@E_p,是我不好,對不起,我心煩意亂 – Federkun

+0

NP之前測試功能和代碼審查添加公共過去了:)。 –

0
class foo{ 
//set the default lenght first 
var $defaultLength = 0; 

function configuration() { 
    $this->defaultLength = 24; 
} 


function setLength($newLength){ 
$this->defaultLength = $newLength; 
} 

function test($length = null) { 
//if length is null assign default 
if($length == null){ 
$length = $this->defaultLength; 
} 

} 

$foo = new foo(); 
$this->configuration(); 
$foo->test(); // int(24) 
$foo->setLength(42); 
$foo->test(); // int(42) 
1

你可以建立你的函數的簽名的數組元素,它們進行過濾,並用默認配置呼叫進行合併所有你需要的功能。 我做了一個簡單的例子,用不同的函數定義2個參數。

class foo{ 

    private $configuration; 

    private function buildDefaultConfiguration() { 
     return $configuration = array(
      'defaultLength' => $this->getDefaultLength(), 
      'defaultWidth' => $this->getDefaultWidth(), 
     ); 
    } 

    public function __construct($givenLength = null, $givenWidth = null) { 
     $givenConfiguration = array(
      'defaultLength' => $givenLength, 
      'defaultWidth' => $givenWidth 
     ); 
     $givenConfiguration = array_filter($givenConfiguration); 
     $defaultConfiguration = $this->buildDefaultConfiguration(); 
     $this->configuration = array_merge($defaultConfiguration, $givenConfiguration); 
    } 

    public function test() 
    { 
     echo $this->configuration['defaultLength'] . ' - ' . $this->configuration['defaultWidth']; 
    } 

    private function getDefaultLength() 
    { 
     return 24; 
    } 

    private function getDefaultWidth() 
    { 
     return 12; 
    } 
} 

$foo1 = new foo(13,14); 
$foo2 = new foo(); 
$foo3 = new foo(null, 66); 
$foo4 = new foo(66); 

$foo1->test(); 
//result is 13 - 14 
$foo2->test(); 
//result is 24 - 12 
$foo3->test(); 
//result is 24 - 66 
$foo4->test(); 
//result is 66 - 12 

您可以檢查現場工作示例here,格式也不是很大,雖然 希望這會幫助你