如何通过调用php类中的函数来定义和声明变量
我习惯于在php函数中设置和声明参数,如下所示: 函数foo($length=24){ 现在我想用一个类函数来实现这一点:如何通过调用php类中的函数来定义和声明变量,php,Php,我习惯于在php函数中设置和声明参数,如下所示: 函数foo($length=24){ 现在我想用一个类函数来实现这一点: class foo{ function configuration() { $this->defaultLength = 24; } function foo() { $this->configuration(); } function test($length = $this->def
class foo{
function configuration() {
$this->defaultLength = 24;
}
function foo() {
$this->configuration();
}
function test($length = $this->defaultLength) {...
致电:
$r = new foo();
$r->test();
我收到以下错误消息:
分析错误:语法错误,在/www/htdo.中意外出现“$this”(T_变量)
如何在php类中定义和声明变量?您不能在函数签名中引用
$this
。您可以执行以下操作:
public function test ($length = null) {
if ($length === null) {
$length = $this->defaultLength;
}
}
您不能在函数签名中引用
$this
。您可以执行以下操作:
public function test ($length = null) {
if ($length === null) {
$length = $this->defaultLength;
}
}
我会这样做:
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)
我会这样做:
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)
您可以使用函数签名的元素构建一个数组,过滤它们,并将其与调用所有所需函数的默认配置合并。 我做了一个简单的例子,用不同的函数定义了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
您可以查看一个实时工作示例,但格式设置并不好
希望这将帮助您您可以使用函数签名的元素构建一个数组,过滤它们,并将其与调用所有所需函数的默认配置合并。 我做了一个简单的例子,用不同的函数定义了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
您可以查看一个实时工作示例,但格式设置并不好
希望这能帮助你为什么他不能?如果他想将它用作
$r->test();
那么$this->defaultLength
可以访问,如果他在同一范围内(类内)声明它。在函数内部,是的。但不在函数签名内部,因为函数签名是在创建任何实例之前读取的。为什么他不能?如果他想将其用作$r->test();
,那么$this->defaultLength
可以访问,如果他在同一范围内(类内)声明它。在函数内部,是的。但不是在函数签名内部,因为函数签名是在创建任何实例之前读取的。@E_p,我的坏,很抱歉我被分散了注意力NP在测试函数和代码审阅通过之前添加公共:)。这真的不是我的日子,我发誓我一直遵循PSR-2规范:P@E_p对不起,我被打扰了dNP在测试函数和代码审查通过之前添加公共:)。这真的不是我的日子,我发誓我一直遵循PSR-2规范:P