简体   繁体   English

php 使用布尔值创建一个新的对象实例

[英]php create a new object instance using a boolean

I'm having a difficult time googling an explanation of a line of php code in a script I'm modifying.我很难在我正在修改的脚本中搜索一行 php 代码的解释。 It is doing the following, where $boolean is a variable set to true or false.它正在执行以下操作,其中 $boolean 是一个设置为 true 或 false 的变量。

$var = new class($boolean); $var = new class($boolean);

What is the significance of the boolean?布尔值的意义是什么? Thanks.谢谢。

This line is creating an instance of class using it's constructor function which is just a function in a class that returns an object.这一行正在使用它的构造函数创建一个class的实例,它只是一个类中返回一个对象的函数。 $boolean is the argument to the constructor function new is a php keyword used for creating instances of classes and $var is the variable to store the returned object in. $boolean是构造函数的参数new是用于创建类实例的 php 关键字, $var是存储返回对象的变量。

The variables passed into the class, will be used to initialize the workings.传递给类的变量将用于初始化工作。 Without seeing the constructor, it's hard to say the significance of the initializing variables.没有看到构造函数,很难说初始化变量的意义。

In object oriented languages, when you define a class you create constructors.在面向对象的语言中,当您定义一个类时,您就创建了构造函数。 The constructors are functions of the type ClassName() or ClassName(Parameters), and you use the new operator with these to create an instance of the class.构造函数是 ClassName() 或 ClassName(Parameters) 类型的函数,您可以将new运算符与它们一起使用来创建类的实例。

The default constructor is an empty one, ie no parameters.默认构造函数是空的,即没有参数。 But you can also create constructors that take parameters.但是您也可以创建带参数的构造函数。 Those are typically used to initialize the member variables of the class.这些通常用于初始化类的成员变量。 So in your example, the boolean is a parameter to one of the constructors.因此,在您的示例中,布尔值是其中一个构造函数的参数。 What exactly it means can only be determined either from the documentation or the code.它的确切含义只能从文档或代码中确定。

Here is an example (not PHP):这是一个示例(不是 PHP):

class SomeClass
{
     private int x;
      public SomeClass() { this.x = 5; }
     public SomeClass(int xx) { this.x = xx; }
}

The empty constructor sets the member x to 5. The constructor with the parameter sets the member x to whatever was passed in. To understand what your boolean is doing, you would have to look at the class definition and see what the constructor that takes in the boolean is doing with it.空构造函数将成员 x 设置为 5。带有参数的构造函数将成员 x 设置为传入的任何内容。要了解您的布尔值在做什么,您必须查看类定义并查​​看接受的构造函数布尔值正在处理它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM