我已经注意到__construct有很多类。我读了一些书,上网浏览了一下,但我找不到我能理解的解释。我只是从面向对象编程开始。
我想知道是否有人能给我一个大概的概念,然后是一个简单的例子,它是如何与PHP一起使用的?
我已经注意到__construct有很多类。我读了一些书,上网浏览了一下,但我找不到我能理解的解释。我只是从面向对象编程开始。
我想知道是否有人能给我一个大概的概念,然后是一个简单的例子,它是如何与PHP一起使用的?
它是用来声明构造函数的。
class Cat
{
function __construct()
{
echo 'meow';
}
}
每当创建类的新实例时,构造函数都会被调用,在这种情况下,构造函数将通过以下行调用:
$cat = new Cat();
在旧的PHP版本中,构造函数也可以使用类名来声明,例如:
class Cat
{
function Cat()
{
echo 'meow';
}
}
__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()是构造函数的方法名。构造函数在对象创建后被调用,是放置初始化代码等的好地方。
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.php
class cat {
function speak() {
return "meow";
}
}
somepage.php
include('class_cat.php');
mycat = new cat;
$speak = cat->speak();
echo $speak;
在@Logan Serman给出的“类猫”的例子中,假设每次你创建一个类“猫”的新对象时,你都希望猫“喵”,而不是等待你调用函数让它喵。
通过这种方式,我的大脑在明确地思考构造函数方法使用隐式的地方,这使得它一开始很难理解。
class Person{
private $fname;
private $lname;
public function __construct($fname,$lname){
$this->fname = $fname;
$this->lname = $lname;
}
}
$objPerson1 = new Person('john','smith');
__construct是一个在使用新对象之前初始化它的方法。 http://php.net/manual/en/language.oop5.decon.php#object.construct
注意:如果子类定义了构造函数,父构造函数不会隐式调用。为了运行父构造函数,需要在子构造函数中调用parent::__construct()。如果子类没有定义构造函数,那么它可以像普通的类方法一样从父类继承(如果它没有声明为private)。
我希望这有助于:
<?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
__construct方法用于在第一次创建对象时传入参数——这被称为“定义构造函数方法”,是一种常见的做法。
然而,构造函数是可选的——所以如果你不想在对象构造时传递任何参数,你就不需要它。
So:
// 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总是在创建新对象时调用,或者在初始化时调用。它适用于对象在使用之前可能需要的任何初始化。__construct方法是类中执行的第一个方法。
class Test
{
function __construct($value1,$value2)
{
echo "Inside Construct";
echo $this->value1;
echo $this->value2;
}
}
//
$testObject = new Test('abc','123');
__construct只是简单地启动一个类。假设您有以下代码;
Class Person {
function __construct() {
echo 'Hello';
}
}
$person = new Person();
//the result 'Hello' will be shown.
我们没有创建另一个函数来响应单词“Hello”。它只是表明关键字__construct在初始化类或对象时非常有用。
构造函数允许在创建对象时初始化对象的属性。
如果你创建了__construct()函数,当你从类中创建对象时,PHP会自动调用这个函数。
https://www.w3schools.com/php/php_oop_constructor.asp
让我先解释一下__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()方法背后的意义。但请注意,这是一个内置的方法或函数