旧式和新式 PHP 构造函数之间的区别
Difference between old-style and new-style PHP constructors
谁能告诉我 "old-style" 对象构造函数与 "new-style" 构造函数有何不同?我正在学习 PHP OOP,我想知道我什么时候阅读旧语法与新语法,并更好地了解 OOP 在 PHP 随着时间的推移发生了怎样的变化。
新款式
class aObject
{
public $name;
public function __construct($name)
{
$this->name = $name;
}
}
"old" 构造函数语法指的是 PHP4。 PHP4 的最后一个版本是在 2008 年,PHP5 的第一个版本是在 2004 年。这是 old-style class 和 new-style 的示例class.
旧 (PHP4)
<?php
class MyOldClass
{
var $foo;
function MyOldClass($foo)
{
$this->foo = $foo;
}
function notAConstructor()
{
/* ... */
}
}
新建(PHP5+)
<?php
class MyNewClass
{
var $foo;
public function __construct($foo)
{
$this->foo = $foo;
}
public function notAConstructor()
{
/* ... */
}
}
您会在这里注意到一些事情。最重要的变化是命名构造函数的规范方式已从 ClassName()
更改为 __construct()
。这为所有 class 构造函数提供了相同的、可预测的名称——这是必要的便利。假设您有一个名为 ParentClass
的 class 和 20 children,每个人都有自己的构造函数。如果您想从每个 child class 调用 parent 构造函数,您可以调用 ParentClass::ParentClass()
。如果您想更改 ParentClass
的名称,则必须更改所有 20 个构造函数调用。但是,使用新方法,您只需调用 parent::__construct()
,这将始终有效,即使 parent class 的名称发生变化。
与此更改相关,PHP5 还引入了 class 析构函数 (__destruct()
),当 object 被销毁(与构造函数相反)。
另一个关键变化是 PHP5 引入了 method and property visibility,它允许对 class 的方法和属性进行某种 "access control"。只有标记为 public
的方法和属性才能从 class 或其 children 之外的上下文访问。以下是这方面的示例:
<?php
class Whosebug
{
/* This can be accessed from anywhere.
*
* You can access it from outside the class:
* $so = new Whosebug();
* $so->publicProperty = 10;
* echo $so->publicProperty;
*
* Or from inside the class:
* $this->publicProperty = 5;
* echo $this->publicProperty;
*/
public $publicProperty = 1;
/* This can be accessed only from methods inside this class.
*
* $this->privateProperty = 5;
* echo $this->privateProperty;
*
* You cannot access it from outside the class, as above.
* Private properties cannot be accessed from child classes,
* either.
*/
private $privateProperty = 2;
/* This can be accessed only from methods inside this class,
* OR a child-class.
*
* $this->protectedProperty = 5;
* echo $this->protectedProperty;
*
* You cannot access it from outside the class, as with public.
* You can, however, access it from a child class.
*/
protected $protectedProperty = 3;
}
现在,方法的工作方式完全相同。您可以将 class 中的函数(方法)标记为 public
、private
或 protected
。在 PHP4 中,所有 class 成员都隐含地 public
。同样,一个构造函数(__construct()
),也可以是public
、private
或protected
!
如果 class 不包含 public 构造函数,它 不能被 [=104 之外的代码实例化=](或者,其children,为protected
)。那么你会如何使用这样的 class 呢?嗯,static methods,当然是:
<?php
class ClassWithPrivateConstructor
{
private $foo;
private function __construct($foo)
{
$this->foo = $foo;
}
public static function fromBar($bar)
{
$foo = do_something_to($bar);
return new self($foo);
}
}
要从其他地方实例化此 class,您需要调用:
$obj = ClassWithPrivateConstructor::fromBar($bar);
当您需要 pre-process 在调用构造函数之前输入,或者当您需要多个采用不同参数的构造函数时,这会很有用。
这个方法名,__construct()
,以及其他以__
开头的方法名,如__get()
、__set()
、__call()
、[=37] =]、__unset()
、__toString()
等被称为magic methods.
PHP5 带来了很多 dramatic changes,但主要是为了保持与 PHP4 代码的兼容性,因此,old-style 构造函数仍然被允许。
PHP7 今年已经发布,构造函数语法没有变化。 PHP7 中与 class
相关的唯一重大变化是允许使用匿名 类(参见 here)。
自 PHP7 起,old-style 构造函数 are officially deprecated(引发 E_DEPRECATED
错误),并将在 PHP8.[=48 中完全删除=]
谁能告诉我 "old-style" 对象构造函数与 "new-style" 构造函数有何不同?我正在学习 PHP OOP,我想知道我什么时候阅读旧语法与新语法,并更好地了解 OOP 在 PHP 随着时间的推移发生了怎样的变化。
新款式
class aObject
{
public $name;
public function __construct($name)
{
$this->name = $name;
}
}
"old" 构造函数语法指的是 PHP4。 PHP4 的最后一个版本是在 2008 年,PHP5 的第一个版本是在 2004 年。这是 old-style class 和 new-style 的示例class.
旧 (PHP4)
<?php
class MyOldClass
{
var $foo;
function MyOldClass($foo)
{
$this->foo = $foo;
}
function notAConstructor()
{
/* ... */
}
}
新建(PHP5+)
<?php
class MyNewClass
{
var $foo;
public function __construct($foo)
{
$this->foo = $foo;
}
public function notAConstructor()
{
/* ... */
}
}
您会在这里注意到一些事情。最重要的变化是命名构造函数的规范方式已从 ClassName()
更改为 __construct()
。这为所有 class 构造函数提供了相同的、可预测的名称——这是必要的便利。假设您有一个名为 ParentClass
的 class 和 20 children,每个人都有自己的构造函数。如果您想从每个 child class 调用 parent 构造函数,您可以调用 ParentClass::ParentClass()
。如果您想更改 ParentClass
的名称,则必须更改所有 20 个构造函数调用。但是,使用新方法,您只需调用 parent::__construct()
,这将始终有效,即使 parent class 的名称发生变化。
与此更改相关,PHP5 还引入了 class 析构函数 (__destruct()
),当 object 被销毁(与构造函数相反)。
另一个关键变化是 PHP5 引入了 method and property visibility,它允许对 class 的方法和属性进行某种 "access control"。只有标记为 public
的方法和属性才能从 class 或其 children 之外的上下文访问。以下是这方面的示例:
<?php
class Whosebug
{
/* This can be accessed from anywhere.
*
* You can access it from outside the class:
* $so = new Whosebug();
* $so->publicProperty = 10;
* echo $so->publicProperty;
*
* Or from inside the class:
* $this->publicProperty = 5;
* echo $this->publicProperty;
*/
public $publicProperty = 1;
/* This can be accessed only from methods inside this class.
*
* $this->privateProperty = 5;
* echo $this->privateProperty;
*
* You cannot access it from outside the class, as above.
* Private properties cannot be accessed from child classes,
* either.
*/
private $privateProperty = 2;
/* This can be accessed only from methods inside this class,
* OR a child-class.
*
* $this->protectedProperty = 5;
* echo $this->protectedProperty;
*
* You cannot access it from outside the class, as with public.
* You can, however, access it from a child class.
*/
protected $protectedProperty = 3;
}
现在,方法的工作方式完全相同。您可以将 class 中的函数(方法)标记为 public
、private
或 protected
。在 PHP4 中,所有 class 成员都隐含地 public
。同样,一个构造函数(__construct()
),也可以是public
、private
或protected
!
如果 class 不包含 public 构造函数,它 不能被 [=104 之外的代码实例化=](或者,其children,为protected
)。那么你会如何使用这样的 class 呢?嗯,static methods,当然是:
<?php
class ClassWithPrivateConstructor
{
private $foo;
private function __construct($foo)
{
$this->foo = $foo;
}
public static function fromBar($bar)
{
$foo = do_something_to($bar);
return new self($foo);
}
}
要从其他地方实例化此 class,您需要调用:
$obj = ClassWithPrivateConstructor::fromBar($bar);
当您需要 pre-process 在调用构造函数之前输入,或者当您需要多个采用不同参数的构造函数时,这会很有用。
这个方法名,__construct()
,以及其他以__
开头的方法名,如__get()
、__set()
、__call()
、[=37] =]、__unset()
、__toString()
等被称为magic methods.
PHP5 带来了很多 dramatic changes,但主要是为了保持与 PHP4 代码的兼容性,因此,old-style 构造函数仍然被允许。
PHP7 今年已经发布,构造函数语法没有变化。 PHP7 中与 class
相关的唯一重大变化是允许使用匿名 类(参见 here)。
自 PHP7 起,old-style 构造函数 are officially deprecated(引发 E_DEPRECATED
错误),并将在 PHP8.[=48 中完全删除=]