Lev*_*evi 194 php constructor
我一直在注意__construct课程.我做了一些阅读和网上冲浪,但我找不到我能理解的解释.我刚刚开始使用OOP.
我想知道是否有人可以给我一个大概它是什么,然后一个简单的例子,它如何与PHP一起使用?
Jan*_*čič 283
__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' );
Run Code Online (Sandbox Code Playgroud)
PHP手册中解释了其他所有内容:点击此处
Rob*_*Rob 49
__construct()是构造函数的方法名称.构造函数在创建后在对象上调用,是放置初始化代码等的好地方.
class Person {
public function __construct() {
// Code called for each new Person we create
}
}
$person = new Person();
Run Code Online (Sandbox Code Playgroud)
构造函数可以以正常方式接受参数,这些参数在创建对象时传递,例如
class Person {
public $name = '';
public function __construct( $name ) {
$this->name = $name;
}
}
$person = new Person( "Joe" );
echo $person->name;
Run Code Online (Sandbox Code Playgroud)
与其他一些语言(例如Java)不同,PHP不支持重载构造函数(即,具有多个接受不同参数的构造函数).您可以使用静态方法实现此效果.
注意:我从(撰写本文时)接受的答案的日志中检索到了这一点.
Log*_*man 18
它是另一种声明构造函数的方法.你也可以使用类名,例如:
class Cat
{
function Cat()
{
echo 'meow';
}
}
Run Code Online (Sandbox Code Playgroud)
和
class Cat
{
function __construct()
{
echo 'meow';
}
}
Run Code Online (Sandbox Code Playgroud)
是等价的.只要创建了类的新实例,就会调用它们,在这种情况下,将使用以下行调用它们:
$cat = new Cat();
Run Code Online (Sandbox Code Playgroud)
小智 16
我认为这对理解构造函数的目的很重要.
即使在阅读了这里的回复后,我花了几分钟才意识到,这就是原因.
我养成了明确编码已启动或发生的所有内容的习惯.换句话说,这将是我的猫类以及我将如何称呼它.
class_cat.php
class cat {
function speak() {
return "meow";
}
}
Run Code Online (Sandbox Code Playgroud)
somepage.php
include('class_cat.php');
mycat = new cat;
$speak = cat->speak();
echo $speak;
Run Code Online (Sandbox Code Playgroud)
在@Logan Serman给出的"类猫"示例中,假设每次创建类"cat"的新对象时,您希望猫"喵"而不是等待您调用函数使其喵喵.
通过这种方式,我的思想明确地考虑了构造函数方法使用的含义,这使得一开始很难理解.
Zam*_*man 10
我希望这个帮助:
<?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();
?>
Run Code Online (Sandbox Code Playgroud)
更多信息您需要访问codecademy.com
该__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);
Run Code Online (Sandbox Code Playgroud)
有关构造函数的更多详细信息,请参阅手册.
小智 7
class Person{
private $fname;
private $lname;
public function __construct($fname,$lname){
$this->fname = $fname;
$this->lname = $lname;
}
}
$objPerson1 = new Person('john','smith');
Run Code Online (Sandbox Code Playgroud)
小智 5
__construct在创建新对象时总是被调用,或者在初始化发生时被调用。它适用于对象使用前可能需要的任何初始化。__construct方法是类中执行的第一个方法。
class Test
{
function __construct($value1,$value2)
{
echo "Inside Construct";
echo $this->value1;
echo $this->value2;
}
}
//
$testObject = new Test('abc','123');
Run Code Online (Sandbox Code Playgroud)