【发布时间】:2010-10-02 03:19:09
【问题描述】:
我在课堂上经常注意到__construct。我做了一些阅读和网上冲浪,但找不到我能理解的解释。我只是从 OOP 开始。
我想知道是否有人可以大致了解它是什么,然后提供一个简单的例子来说明它是如何与 PHP 一起使用的?
【问题讨论】:
标签: php constructor
我在课堂上经常注意到__construct。我做了一些阅读和网上冲浪,但找不到我能理解的解释。我只是从 OOP 开始。
我想知道是否有人可以大致了解它是什么,然后提供一个简单的例子来说明它是如何与 PHP 一起使用的?
【问题讨论】:
标签: php constructor
让我先解释一下__construct(),而不先使用该方法... 关于__construct() 要了解的一点是它是一个内置函数,那么让我在PHP 中将其称为方法。就像我们有 print_r() 用于程序一样,__construct() 是面向 OOP 的内置。
话虽如此,让我们来探讨一下为什么要使用这个名为__construct() 的函数。
/*=======Class without __construct()========*/
class ThaddLawItSolution
{
public $description;
public $url;
public $ourServices;
/*===Let us initialize a value to our property via the method set_name()==== */
public function setName($anything,$anythingYouChoose,$anythingAgainYouChoose)
{
$this->description=$anything;
$this->url=$anythingYouChoose;
$this->ourServices=$anythingAgainYouChoose;
}
/*===Let us now display it on our browser peacefully without stress===*/
public function displayOnBrowser()
{
echo "$this->description is a technological company in Nigeria and our domain name is actually $this->url.Please contact us today for our services:$this->ourServices";
}
}
//Creating an object of the class ThaddLawItSolution
$project=new ThaddLawItSolution;
//=======Assigning Values to those properties via the method created====//
$project->setName("Thaddlaw IT Solution", "https://www.thaddlaw.com", "Please view our website");
//===========Let us now display it on the browser=======
$project->displayOnBrowser();
__construct() 让您的生活变得非常轻松,想象一下我通过该方法为这些属性分配值所花费的时间。从上面的代码中,我首先创建了一个对象,然后将值分配给第二个属性,最后在浏览器上显示它。但是在创建对象时使用__construct(),即$project= new ThaddLawItSolution;,您可以在创建对象时立即为该方法分配值,即
$project=new ThaddLawItSolution("Thaddlaw IT Solution", "https://www.thaddlaw.com","Please view our website");
//===现在让我们使用 __constructor=====
只需删除名为setName 的方法并放入__construct(); 并在创建对象时立即分配值。这就是整个__construct() 方法背后的要点。但请注意,这是一个内置的方法或函数
【讨论】:
构造函数允许您在创建对象时初始化对象的属性。
如果你创建了一个 __construct() 函数,当你从一个类创建一个对象时,PHP 会自动调用这个函数。
【讨论】:
我认为这对于理解构造函数的目的很重要。
即使在阅读了这里的回复之后,我也花了几分钟才意识到这就是原因。
我已经养成了对所有启动或发生的事情进行显式编码的习惯。换句话说,这将是我的猫类以及我将如何称呼它。
class_cat.php
class cat {
function speak() {
return "meow";
}
}
somepage.php
include('class_cat.php');
mycat = new cat;
$speak = cat->speak();
echo $speak;
在@Logan Serman 给出的“类猫”示例中,假设每次创建类“猫”的新对象时,您希望猫“喵喵叫”而不是等待您调用函数来制作它喵。
这样一来,我的大脑就在明确地思考构造方法在哪里使用了隐式,这让我一开始很难理解。
【讨论】:
__construct 只是启动一个类。假设你有以下代码;
Class Person {
function __construct() {
echo 'Hello';
}
}
$person = new Person();
//the result 'Hello' will be shown.
我们没有创建另一个函数来回显“Hello”这个词。它只是表明关键字 __construct 在初始化类或对象时非常有用。
【讨论】:
__construct 方法用于在您第一次创建对象时传入参数——这称为“定义构造方法”,这是一种常见的做法。
然而,构造函数是可选的——所以如果你不想在对象构造时传递任何参数,你就不需要它。
所以:
// Create a new class, and include a __construct method
class Task {
public $title;
public $description;
public function __construct($title, $description){
$this->title = $title;
$this->description = $description;
}
}
// Create a new object, passing in a $title and $description
$task = new Task('Learn OOP','This is a description');
// Try it and see
var_dump($task->title, $task->description);
有关什么是构造函数的更多详细信息,请参阅the manual。
【讨论】:
__construct 总是在创建新对象时调用,或者在初始化发生时调用它们。它适用于对象在使用之前可能需要的任何初始化。 __construct 方法是类中第一个执行的方法。
class Test
{
function __construct($value1,$value2)
{
echo "Inside Construct";
echo $this->value1;
echo $this->value2;
}
}
//
$testObject = new Test('abc','123');
【讨论】:
__construct 是在 PHP5 中引入的,它是定义您的构造函数的正确方法(在 PHP4 中,您使用类的名称作为构造函数)。
您不需要在类中定义构造函数,但如果您希望在对象构造中传递任何参数,那么您需要一个。
一个例子可以是这样的:
class Database {
protected $userName;
protected $password;
protected $dbName;
public function __construct ( $UserName, $Password, $DbName ) {
$this->userName = $UserName;
$this->password = $Password;
$this->dbName = $DbName;
}
}
// and you would use this as:
$db = new Database ( 'user_name', 'password', 'database_name' );
PHP 手册中解释了其他所有内容:click here
【讨论】:
我希望这个帮助:
<?php
// The code below creates the class
class Person {
// Creating some properties (variables tied to an object)
public $isAlive = true;
public $firstname;
public $lastname;
public $age;
// Assigning the values
public function __construct($firstname, $lastname, $age) {
$this->firstname = $firstname;
$this->lastname = $lastname;
$this->age = $age;
}
// Creating a method (function tied to an object)
public function greet() {
return "Hello, my name is " . $this->firstname . " " . $this->lastname . ". Nice to meet you! :-)";
}
}
// Creating a new person called "boring 12345", who is 12345 years old ;-)
$me = new Person('boring', '12345', 12345);
// Printing out, what the greet method returns
echo $me->greet();
?>
欲了解更多信息,您需要前往codecademy.com
【讨论】:
注意:如果子类定义了构造函数,则不会隐式调用父构造函数。为了运行父构造函数,需要在子构造函数中调用parent::__construct()。如果子类没有定义构造函数,那么它可以像普通类方法一样从父类继承(如果它没有声明为私有)。
【讨论】:
__construct 是一种在使用新对象之前对其进行初始化的方法。
http://php.net/manual/en/language.oop5.decon.php#object.construct
【讨论】:
构造函数是在类实例化时自动调用的方法。这意味着在没有单独的方法调用的情况下处理构造函数的内容。类关键字括号的内容被传递给构造方法。
【讨论】:
class Person{
private $fname;
private $lname;
public function __construct($fname,$lname){
$this->fname = $fname;
$this->lname = $lname;
}
}
$objPerson1 = new Person('john','smith');
【讨论】:
__construct() 是constructor 的方法名称。构造函数在对象创建后被调用,是放置初始化代码等的好地方。
class Person {
public function __construct() {
// Code called for each new Person we create
}
}
$person = new Person();
构造函数可以以正常方式接受参数,这些参数在创建对象时传递,例如
class Person {
public $name = '';
public function __construct( $name ) {
$this->name = $name;
}
}
$person = new Person( "Joe" );
echo $person->name;
与其他一些语言(例如 Java)不同,PHP 不支持重载构造函数(即具有多个接受不同参数的构造函数)。您可以使用静态方法来实现此效果。
注意:我是从(撰写本文时)已接受答案的日志中检索到的。
【讨论】:
我相信函数__construct () {...} 是一段可以反复重用的代码来代替TheActualFunctionName () {...}。
如果您更改类名,则不必在代码中进行更改,因为通用 __construct 始终引用实际的类名……不管它是什么。
你的代码少了……还是?
【讨论】:
它是另一种声明构造函数的方式。您也可以使用类名,例如:
class Cat
{
function Cat()
{
echo 'meow';
}
}
和
class Cat
{
function __construct()
{
echo 'meow';
}
}
是等价的。每当创建类的新实例时都会调用它们,在这种情况下,将使用以下行调用它们:
$cat = new Cat();
【讨论】:
Cat() 和 __cosntruct() 在此上下文中执行相同的操作,但您必须小心:如果类 Cat 位于命名空间 Cat() 中,则自 PHP 5.3 起将不会被视为构造函数.3. php.net/manual/en/language.oop5.decon.php