什么是__construct使用的function?

我一直注意到__construct了很多类。 我做了一些小小的阅读和浏览网页,但是我找不到我能理解的解释。 我刚刚开始OOP。

我想知道如果有人能给我一个什么是一个大概的想法,然后一个简单的例子,它是如何使用PHP?

__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手册中解释: 点击这里

__construct()是构造函数的方法名称。 构造函数在被创build后会在对象上调用,并且是放置初始化代码等的好地方。

 class Person { public function __construct() { // Code called for each new Person we create } } $person = new Person(); 

构造函数可以以正常方式接受参数,这些参数在创build对象时传递,例如

 class Person { public $name = ''; public function __construct( $name ) { $this->name = $name; } } $person = new Person( "Joe" ); echo $person->name; 

与其他一些语言(例如Java)不同,PHP不支持重载构造函数(也就是说,有多个接受不同参数的构造函数)。 你可以使用静态方法来达到这个效果。

注意 :我从(在写这篇文章的时候)接受的答案的日志中检索到这个。

它的另一种方式来声明构造函数。 你也可以使用类名,例如:

 class Cat { function Cat() { echo 'meow'; } } 

 class Cat { function __construct() { echo 'meow'; } } 

是等同的。 当这个类的新实例被创build时,它们被调用,在这种情况下,它们将被调用:

 $cat = new Cat(); 

我认为这对于理解构造函数的目的很重要。
即使在阅读了这里的回答之后,我花了几分钟才意识到,这是原因。
我已经习惯了明确地编码所发起或发生的一切事情。 换句话说,这将是我的猫类,我将如何称呼它。

class_cat.php

 class cat { function speak() { echo "meow"; } } 

somepage.php

 include('class_cat.php'); mycat = new cat; $speak = cat->speak(); echo $speak; 

在@Logan Serman给出的“class cat”示例中,假设每次创build一个“cat”类的新对象时,都希望cat“喵”,而不是等着你调用这个函数。

这样,我的脑子就明白地想到了构造函数方法在什么地方使用隐含性,这使得开始很难理解。

构造函数是在类实例化时自动调用的方法。 这意味着构造函数的内容在没有单独的方法调用的情况下被处理。 类关键字括号的内容传递给构造方法。

我希望这个帮助:

 <?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

 class Person{ private $fname; private $lname; public function __construct($fname,$lname){ $this->fname = $fname; $this->lname = $lname; } } $objPerson1 = new Person('john','smith'); 

__construct方法用于在首次创build对象时传入参数 – 这是常见的事情。 如果您不想在创build时传递任何参数,则不需要它。

所以:

 // 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); 

我相信函数__construct () {...}是一段代码,可以一次又一次地重复使用,替代TheActualFunctionName () {...} 。 如果您更改CLASS名称,则不必在代码中进行更改,因为通用__construct始终指向实际的类名称,无论它是什么。 你代码less…或?

__construct是在使用之前初始化新对象的方法。
http://php.net/manual/en/language.oop5.decon.php#object.construct

注:如果子类定义了构造函数,则父构造函数不会隐式调用。 为了运行父构造函数,需要在子构造函数中调用parent::__construct() 。 如果孩子没有定义一个构造函数,那么它就像普通的类方法(如果它没有被声明为私有的)一样可以从父类inheritance。

创build新对象时始终调用__construct,或者在初始化发生时调用__construct。它适用于在使用对象之前可能需要的任何初始化。 __construct方法是在类中执行的第一个方法。

  class Test { function __construct($value1,$value2) { echo "Inside Construct"; echo $this->value1; echo $this->value2; } } // $testObject = new Test('abc','123');