Hướng dẫn php should constructor be public or private - php nên hàm tạo là công khai hay riêng tư

Có một số kịch bản mà bạn có thể muốn làm cho hàm tạo của mình riêng tư. Lý do phổ biến là trong một số trường hợp, bạn không muốn mã bên ngoài gọi trực tiếp cho hàm tạo của mình, nhưng buộc nó phải sử dụng một phương thức khác để có được một thể hiện của lớp của bạn.

Mô hình đơn lẻ

Bạn chỉ muốn một phiên bản duy nhất của lớp của bạn tồn tại:

class Singleton
{
    private static $instance = null;

    private function __construct()
    {
    }

    public static function getInstance()
    {
        if (self::$instance === null) {
            self::$instance = new self();
        }

        return self::$instance;
    }
}

Phương pháp nhà máy

Bạn muốn cung cấp một số phương thức để tạo một thể hiện của lớp của bạn và/hoặc bạn muốn kiểm soát cách tạo phiên bản của bạn, bởi vì một số kiến ​​thức nội bộ của hàm tạo là cần thiết để gọi đúng cách:

class Decimal
{
    private $value; // constraint: a non-empty string of digits
    private $scale; // constraint: an integer >= 0

    private function __construct($value, $scale = 0)
    {
        // Value and scale are expected to be validated here.
        // Because the constructor is private, it can only be called from within the class,
        // so we can avoid to perform validation at this step, and just trust the caller.

        $this->value = $value;
        $this->scale = $scale;
    }

    public static function zero()
    {
        return new self('0');
    }

    public static function fromString($string)
    {
        // Perform sanity checks on the string, and compute the value & scale

        // ...

        return new self($value, $scale);
    }
}

Ví dụ đơn giản hóa từ việc triển khai gạch/toán học lớn

Khi nào một nhà xây dựng nên được riêng tư?

Các hàm tạo riêng được sử dụng để ngăn chặn việc tạo các trường hợp của một lớp khi không có trường hoặc phương thức thể hiện, chẳng hạn như lớp toán học hoặc khi một phương thức được gọi để có được một thể hiện của một lớp. Nếu tất cả các phương pháp trong lớp là tĩnh, hãy xem xét việc làm cho lớp hoàn chỉnh tĩnh.(mixed ...$values = ""): void

Người xây dựng

Lưu ý: Các hàm tạo cha mẹ không được gọi là ngầm nếu lớp con định nghĩa một hàm tạo. Để chạy một hàm tạo cha mẹ, một cuộc gọi đến cha mẹ :: __ construct () trong hàm tạo con là bắt buộc. Nếu trẻ không định nghĩa một hàm tạo thì nó có thể được kế thừa từ lớp cha giống như một phương thức lớp bình thường (nếu nó không được tuyên bố là riêng tư).: Parent constructors are not called implicitly if the child class defines a constructor. In order to run a parent constructor, a call to parent::__construct() within the child constructor is required. If the child does not define a constructor then it may be inherited from the parent class just like a normal class method (if it was not declared as private).

Ví dụ số 1 người xây dựng trong kế thừa

class BaseClass {
    function 
__construct() {
        print 
"In BaseClass constructor\n";
    }
}

class

SubClass extends BaseClass {
    function 
__construct() {
        
parent::__construct();
        print 
"In SubClass constructor\n";
    }
}

class

OtherSubClass extends BaseClass {
    
// inherits BaseClass's constructor
}// In BaseClass constructor
$obj = new BaseClass();// In BaseClass constructor
// In SubClass constructor
$obj = new SubClass();// In BaseClass constructor
$obj = new OtherSubClass();
?>

Không giống như các phương thức khác, __construct () được miễn trừ khỏi các quy tắc tương thích chữ ký thông thường khi được mở rộng.

Các hàm tạo là các phương pháp thông thường được gọi trong quá trình khởi tạo đối tượng tương ứng của chúng. Như vậy, chúng có thể xác định một số lượng đối số tùy ý, có thể được yêu cầu, có thể có một loại và có thể có giá trị mặc định. Các đối số của hàm tạo được gọi bằng cách đặt các đối số trong ngoặc đơn sau tên lớp.

Ví dụ #2 sử dụng các đối số của hàm tạo

class Point {
    protected 
int $x;
    protected 
int $y;

    public function

__construct(int $xint $y 0) {
        
$this->$x;
        
$this->$y;
    }
}
// Pass both parameters.
$p1 = new Point(45);
// Pass only the required parameter. $y will take its default value of 0.
$p2 = new Point(4);
// With named parameters (as of PHP 8.0):
$p3 = new Point(y5x4);
?>

Nếu một lớp không có hàm tạo, hoặc hàm tạo không có đối số bắt buộc, dấu ngoặc đơn có thể bị bỏ qua.

Nhà xây dựng kiểu cũ

Trước Php 8.0.0, các lớp trong không gian tên toàn cầu sẽ diễn giải một phương pháp được đặt tên giống như lớp như một hàm tạo kiểu cũ. Cú pháp đó không được chấp nhận và sẽ dẫn đến lỗi E_DEPRECATED nhưng vẫn gọi chức năng đó là một hàm tạo. Nếu cả __construct () và một phương thức cùng tên được xác định, __construct () sẽ được gọi.E_DEPRECATED error but still call that function as a constructor. If both __construct() and a same-name method are defined, __construct() will be called.

Trong các lớp theo tên, hoặc bất kỳ lớp nào là của Php 8.0.0, một phương pháp có tên giống như lớp không bao giờ có bất kỳ ý nghĩa đặc biệt nào.

Luôn luôn sử dụng __construct () trong mã mới.

Mới trong khởi tạo

Kể từ Php 8.1.0, các đối tượng có thể được sử dụng làm giá trị tham số mặc định, các biến tĩnh và hằng số toàn cầu, cũng như trong các đối số thuộc tính. Các đối tượng cũng có thể được truyền để xác định () ngay bây giờ.define() now.

Ghi chú::

Việc sử dụng tên lớp động hoặc không chuỗi hoặc lớp ẩn danh không được phép. Việc sử dụng giải nén đối số không được phép. Việc sử dụng các biểu thức không được hỗ trợ làm đối số không được phép.

Ví dụ số 4 sử dụng mới trong bộ khởi tạo

class Decimal
{
    private $value; // constraint: a non-empty string of digits
    private $scale; // constraint: an integer >= 0

    private function __construct($value, $scale = 0)
    {
        // Value and scale are expected to be validated here.
        // Because the constructor is private, it can only be called from within the class,
        // so we can avoid to perform validation at this step, and just trust the caller.

        $this->value = $value;
        $this->scale = $scale;
    }

    public static function zero()
    {
        return new self('0');
    }

    public static function fromString($string)
    {
        // Perform sanity checks on the string, and compute the value & scale

        // ...

        return new self($value, $scale);
    }
}
0

class Decimal
{
    private $value; // constraint: a non-empty string of digits
    private $scale; // constraint: an integer >= 0

    private function __construct($value, $scale = 0)
    {
        // Value and scale are expected to be validated here.
        // Because the constructor is private, it can only be called from within the class,
        // so we can avoid to perform validation at this step, and just trust the caller.

        $this->value = $value;
        $this->scale = $scale;
    }

    public static function zero()
    {
        return new self('0');
    }

    public static function fromString($string)
    {
        // Perform sanity checks on the string, and compute the value & scale

        // ...

        return new self($value, $scale);
    }
}
1

class Decimal
{
    private $value; // constraint: a non-empty string of digits
    private $scale; // constraint: an integer >= 0

    private function __construct($value, $scale = 0)
    {
        // Value and scale are expected to be validated here.
        // Because the constructor is private, it can only be called from within the class,
        // so we can avoid to perform validation at this step, and just trust the caller.

        $this->value = $value;
        $this->scale = $scale;
    }

    public static function zero()
    {
        return new self('0');
    }

    public static function fromString($string)
    {
        // Perform sanity checks on the string, and compute the value & scale

        // ...

        return new self($value, $scale);
    }
}
2

Phương pháp tạo tĩnh

PHP chỉ hỗ trợ một hàm tạo đơn cho mỗi lớp. Tuy nhiên, trong một số trường hợp, có thể mong muốn cho phép một đối tượng được xây dựng theo những cách khác nhau với các đầu vào khác nhau. Cách được khuyến nghị để làm như vậy là bằng cách sử dụng các phương thức tĩnh làm trình bao gồm hàm tạo.

Ví dụ #5 sử dụng các phương thức tạo tĩnh

class Decimal
{
    private $value; // constraint: a non-empty string of digits
    private $scale; // constraint: an integer >= 0

    private function __construct($value, $scale = 0)
    {
        // Value and scale are expected to be validated here.
        // Because the constructor is private, it can only be called from within the class,
        // so we can avoid to perform validation at this step, and just trust the caller.

        $this->value = $value;
        $this->scale = $scale;
    }

    public static function zero()
    {
        return new self('0');
    }

    public static function fromString($string)
    {
        // Perform sanity checks on the string, and compute the value & scale

        // ...

        return new self($value, $scale);
    }
}
3

class Decimal
{
    private $value; // constraint: a non-empty string of digits
    private $scale; // constraint: an integer >= 0

    private function __construct($value, $scale = 0)
    {
        // Value and scale are expected to be validated here.
        // Because the constructor is private, it can only be called from within the class,
        // so we can avoid to perform validation at this step, and just trust the caller.

        $this->value = $value;
        $this->scale = $scale;
    }

    public static function zero()
    {
        return new self('0');
    }

    public static function fromString($string)
    {
        // Perform sanity checks on the string, and compute the value & scale

        // ...

        return new self($value, $scale);
    }
}
4

class Decimal
{
    private $value; // constraint: a non-empty string of digits
    private $scale; // constraint: an integer >= 0

    private function __construct($value, $scale = 0)
    {
        // Value and scale are expected to be validated here.
        // Because the constructor is private, it can only be called from within the class,
        // so we can avoid to perform validation at this step, and just trust the caller.

        $this->value = $value;
        $this->scale = $scale;
    }

    public static function zero()
    {
        return new self('0');
    }

    public static function fromString($string)
    {
        // Perform sanity checks on the string, and compute the value & scale

        // ...

        return new self($value, $scale);
    }
}
5

Chất xây dựng có thể được làm riêng hoặc được bảo vệ để ngăn chặn nó được gọi là bên ngoài. Nếu vậy, chỉ có một phương pháp tĩnh sẽ có thể khởi tạo lớp. Bởi vì chúng nằm trong cùng một định nghĩa lớp, họ có quyền truy cập vào các phương thức riêng tư, ngay cả khi không có cùng một thể hiện đối tượng. Hàm tạo riêng là tùy chọn và có thể hoặc không có ý nghĩa tùy thuộc vào trường hợp sử dụng.

Ba phương pháp tĩnh công khai sau đó chứng minh các cách khác nhau để khởi tạo đối tượng.

  • class Decimal
    {
        private $value; // constraint: a non-empty string of digits
        private $scale; // constraint: an integer >= 0
    
        private function __construct($value, $scale = 0)
        {
            // Value and scale are expected to be validated here.
            // Because the constructor is private, it can only be called from within the class,
            // so we can avoid to perform validation at this step, and just trust the caller.
    
            $this->value = $value;
            $this->scale = $scale;
        }
    
        public static function zero()
        {
            return new self('0');
        }
    
        public static function fromString($string)
        {
            // Perform sanity checks on the string, and compute the value & scale
    
            // ...
    
            return new self($value, $scale);
        }
    }
    
    6 lấy các tham số chính xác cần thiết, sau đó tạo đối tượng bằng cách gọi hàm tạo và trả về kết quả.
  • class Decimal
    {
        private $value; // constraint: a non-empty string of digits
        private $scale; // constraint: an integer >= 0
    
        private function __construct($value, $scale = 0)
        {
            // Value and scale are expected to be validated here.
            // Because the constructor is private, it can only be called from within the class,
            // so we can avoid to perform validation at this step, and just trust the caller.
    
            $this->value = $value;
            $this->scale = $scale;
        }
    
        public static function zero()
        {
            return new self('0');
        }
    
        public static function fromString($string)
        {
            // Perform sanity checks on the string, and compute the value & scale
    
            // ...
    
            return new self($value, $scale);
        }
    }
    
    7 chấp nhận chuỗi JSON và thực hiện một số xử lý trước trên chính nó để chuyển đổi nó thành định dạng mong muốn của hàm tạo. Sau đó, nó trả về đối tượng mới.
  • class Decimal
    {
        private $value; // constraint: a non-empty string of digits
        private $scale; // constraint: an integer >= 0
    
        private function __construct($value, $scale = 0)
        {
            // Value and scale are expected to be validated here.
            // Because the constructor is private, it can only be called from within the class,
            // so we can avoid to perform validation at this step, and just trust the caller.
    
            $this->value = $value;
            $this->scale = $scale;
        }
    
        public static function zero()
        {
            return new self('0');
        }
    
        public static function fromString($string)
        {
            // Perform sanity checks on the string, and compute the value & scale
    
            // ...
    
            return new self($value, $scale);
        }
    }
    
    8 chấp nhận chuỗi XML, tiền xử lý và sau đó tạo một đối tượng trần. Hàm tạo vẫn được gọi, nhưng vì tất cả các tham số là tùy chọn phương thức bỏ qua chúng. Sau đó, nó gán các giá trị cho các thuộc tính đối tượng trực tiếp trước khi trả về kết quả.

Trong cả ba trường hợp, từ khóa

class Decimal
{
    private $value; // constraint: a non-empty string of digits
    private $scale; // constraint: an integer >= 0

    private function __construct($value, $scale = 0)
    {
        // Value and scale are expected to be validated here.
        // Because the constructor is private, it can only be called from within the class,
        // so we can avoid to perform validation at this step, and just trust the caller.

        $this->value = $value;
        $this->scale = $scale;
    }

    public static function zero()
    {
        return new self('0');
    }

    public static function fromString($string)
    {
        // Perform sanity checks on the string, and compute the value & scale

        // ...

        return new self($value, $scale);
    }
}
9 được dịch thành tên của lớp, mã được sử dụng. Trong trường hợp này, ...$values0.

Người phá hủy

__destruct (): Void(): void

PHP sở hữu một khái niệm phá hủy tương tự như các ngôn ngữ hướng đối tượng khác, chẳng hạn như C ++. Phương pháp phá hủy sẽ được gọi ngay khi không có tài liệu tham khảo nào khác đến một đối tượng cụ thể hoặc theo bất kỳ thứ tự nào trong chuỗi tắt.

Ví dụ #6 Ví dụ về Destruction

...$values1

...$values2

...$values3

Giống như các nhà xây dựng, các hàm hủy của cha mẹ sẽ không được gọi là ngầm bởi động cơ. Để chạy một kẻ hủy diệt cha mẹ, người ta sẽ phải gọi rõ ràng cha mẹ :: __ sestruct () trong cơ thể phá hủy. Cũng giống như các nhà xây dựng, một lớp con có thể thừa hưởng chất hủy diệt của cha mẹ nếu nó không tự thực hiện một.parent::__destruct() in the destructor body. Also like constructors, a child class may inherit the parent's destructor if it does not implement one itself.

Bộ hủy sẽ được gọi ngay cả khi việc thực thi tập lệnh được dừng bằng EXIT (). Gọi EXIT () trong một bộ hủy sẽ ngăn các thói quen tắt còn lại thực thi.exit(). Calling exit() in a destructor will prevent the remaining shutdown routines from executing.

Ghi chú::

Việc sử dụng tên lớp động hoặc không chuỗi hoặc lớp ẩn danh không được phép. Việc sử dụng giải nén đối số không được phép. Việc sử dụng các biểu thức không được hỗ trợ làm đối số không được phép.

Ghi chú::

Cố gắng ném một ngoại lệ từ một kẻ phá hủy (được gọi trong thời gian chấm dứt kịch bản) gây ra lỗi nghiêm trọng.

David Dot Scourfield tại Llynfi Dot Co Dot Uk ¶

11 năm trước

...$values4

...$values5

...$values6

...$values7

...$values8

mmulej tại gmail dot com ¶

6 tháng trước

...$values9

class BaseClass {
    function 
__construct() {
        print 
"In BaseClass constructor\n";
    }
}
0

class BaseClass {
    function 
__construct() {
        print 
"In BaseClass constructor\n";
    }
}
1

class BaseClass {
    function 
__construct() {
        print 
"In BaseClass constructor\n";
    }
}
2

class BaseClass {
    function 
__construct() {
        print 
"In BaseClass constructor\n";
    }
}
3

class BaseClass {
    function 
__construct() {
        print 
"In BaseClass constructor\n";
    }
}
4

Domger tại Freenet Dot de ¶

5 năm trước

class BaseClass {
    function 
__construct() {
        print 
"In BaseClass constructor\n";
    }
}
5

class BaseClass {
    function 
__construct() {
        print 
"In BaseClass constructor\n";
    }
}
6

class BaseClass {
    function 
__construct() {
        print 
"In BaseClass constructor\n";
    }
}
7

class BaseClass {
    function 
__construct() {
        print 
"In BaseClass constructor\n";
    }
}
8

class BaseClass {
    function 
__construct() {
        print 
"In BaseClass constructor\n";
    }
}
4

IWWP tại Outlook Dot Com ¶

2 năm trước

class0

class1

class2

class3

class4

class5

class6

class BaseClass {
    function 
__construct() {
        print 
"In BaseClass constructor\n";
    }
}
4

lách ¶

13 năm trước

class8

class9

SubClass extends BaseClass {
    function 
__construct() {
        
parent::__construct();
        print 
"In SubClass constructor\n";
    }
}

class

OtherSubClass extends BaseClass {
    
// inherits BaseClass's constructor
}// In BaseClass constructor
$obj = new BaseClass();// In BaseClass constructor
// In SubClass constructor
$obj = new SubClass();// In BaseClass constructor
$obj = new OtherSubClass();
?>
0

SubClass extends BaseClass {
    function 
__construct() {
        
parent::__construct();
        print 
"In SubClass constructor\n";
    }
}

class

OtherSubClass extends BaseClass {
    
// inherits BaseClass's constructor
}// In BaseClass constructor
$obj = new BaseClass();// In BaseClass constructor
// In SubClass constructor
$obj = new SubClass();// In BaseClass constructor
$obj = new OtherSubClass();
?>
1

SubClass extends BaseClass {
    function 
__construct() {
        
parent::__construct();
        print 
"In SubClass constructor\n";
    }
}

class

OtherSubClass extends BaseClass {
    
// inherits BaseClass's constructor
}// In BaseClass constructor
$obj = new BaseClass();// In BaseClass constructor
// In SubClass constructor
$obj = new SubClass();// In BaseClass constructor
$obj = new OtherSubClass();
?>
2

SubClass extends BaseClass {
    function 
__construct() {
        
parent::__construct();
        print 
"In SubClass constructor\n";
    }
}

class

OtherSubClass extends BaseClass {
    
// inherits BaseClass's constructor
}// In BaseClass constructor
$obj = new BaseClass();// In BaseClass constructor
// In SubClass constructor
$obj = new SubClass();// In BaseClass constructor
$obj = new OtherSubClass();
?>
3

SubClass extends BaseClass {
    function 
__construct() {
        
parent::__construct();
        print 
"In SubClass constructor\n";
    }
}

class

OtherSubClass extends BaseClass {
    
// inherits BaseClass's constructor
}// In BaseClass constructor
$obj = new BaseClass();// In BaseClass constructor
// In SubClass constructor
$obj = new SubClass();// In BaseClass constructor
$obj = new OtherSubClass();
?>
4

SubClass extends BaseClass {
    function 
__construct() {
        
parent::__construct();
        print 
"In SubClass constructor\n";
    }
}

class

OtherSubClass extends BaseClass {
    
// inherits BaseClass's constructor
}// In BaseClass constructor
$obj = new BaseClass();// In BaseClass constructor
// In SubClass constructor
$obj = new SubClass();// In BaseClass constructor
$obj = new OtherSubClass();
?>
5

SubClass extends BaseClass {
    function 
__construct() {
        
parent::__construct();
        print 
"In SubClass constructor\n";
    }
}

class

OtherSubClass extends BaseClass {
    
// inherits BaseClass's constructor
}// In BaseClass constructor
$obj = new BaseClass();// In BaseClass constructor
// In SubClass constructor
$obj = new SubClass();// In BaseClass constructor
$obj = new OtherSubClass();
?>
6

class BaseClass {
    function 
__construct() {
        print 
"In BaseClass constructor\n";
    }
}
4

prieler tại ABM Dot tại ¶

15 năm trước

SubClass extends BaseClass {
    function 
__construct() {
        
parent::__construct();
        print 
"In SubClass constructor\n";
    }
}

class

OtherSubClass extends BaseClass {
    
// inherits BaseClass's constructor
}// In BaseClass constructor
$obj = new BaseClass();// In BaseClass constructor
// In SubClass constructor
$obj = new SubClass();// In BaseClass constructor
$obj = new OtherSubClass();
?>
8

SubClass extends BaseClass {
    function 
__construct() {
        
parent::__construct();
        print 
"In SubClass constructor\n";
    }
}

class

OtherSubClass extends BaseClass {
    
// inherits BaseClass's constructor
}// In BaseClass constructor
$obj = new BaseClass();// In BaseClass constructor
// In SubClass constructor
$obj = new SubClass();// In BaseClass constructor
$obj = new OtherSubClass();
?>
9

class Point {
    protected 
int $x;
    protected 
int $y;
0

class Point {
    protected 
int $x;
    protected 
int $y;
1

Yousef Ismaeil cliprz [at] gmail [dot] com ¶

9 năm trước

class Point {
    protected 
int $x;
    protected 
int $y;
2

class Point {
    protected 
int $x;
    protected 
int $y;
3

class Point {
    protected 
int $x;
    protected 
int $y;
4

Mỗi Persson ¶

10 năm trước

class Point {
    protected 
int $x;
    protected 
int $y;
5

class Point {
    protected 
int $x;
    protected 
int $y;
6

class Point {
    protected 
int $x;
    protected 
int $y;
7

class Point {
    protected 
int $x;
    protected 
int $y;
8

Jonathon Hibbard ¶

12 năm trước

class Point {
    protected 
int $x;
    protected 
int $y;
9

    public function0

    public function1

    public function2

Bolshun tại Mail Dot Ru ¶

14 năm trước

    public function3

David tại Synatree Dot Com ¶

14 năm trước

    public function4

    public function5

    public function6

    public function7

    public function8

    public function9

class Point {
    protected 
int $x;
    protected 
int $y;
3

__construct(int $xint $y 0) {
        
$this->$x;
        
$this->$y;
    }
}
// Pass both parameters.
$p1 = new Point(45);
// Pass only the required parameter. $y will take its default value of 0.
$p2 = new Point(4);
// With named parameters (as of PHP 8.0):
$p3 = new Point(y5x4);
?>
1

__construct(int $xint $y 0) {
        
$this->$x;
        
$this->$y;
    }
}
// Pass both parameters.
$p1 = new Point(45);
// Pass only the required parameter. $y will take its default value of 0.
$p2 = new Point(4);
// With named parameters (as of PHP 8.0):
$p3 = new Point(y5x4);
?>
2

__construct(int $xint $y 0) {
        
$this->$x;
        
$this->$y;
    }
}
// Pass both parameters.
$p1 = new Point(45);
// Pass only the required parameter. $y will take its default value of 0.
$p2 = new Point(4);
// With named parameters (as of PHP 8.0):
$p3 = new Point(y5x4);
?>
3

__construct(int $xint $y 0) {
        
$this->$x;
        
$this->$y;
    }
}
// Pass both parameters.
$p1 = new Point(45);
// Pass only the required parameter. $y will take its default value of 0.
$p2 = new Point(4);
// With named parameters (as of PHP 8.0):
$p3 = new Point(y5x4);
?>
4

__construct(int $xint $y 0) {
        
$this->$x;
        
$this->$y;
    }
}
// Pass both parameters.
$p1 = new Point(45);
// Pass only the required parameter. $y will take its default value of 0.
$p2 = new Point(4);
// With named parameters (as of PHP 8.0):
$p3 = new Point(y5x4);
?>
5

class BaseClass {
    function 
__construct() {
        print 
"In BaseClass constructor\n";
    }
}
4

David tại Synatree Dot Com ¶

ziggy khi bắt đầu chấm bụi ¶

__construct(int $xint $y 0) {
        
$this->$x;
        
$this->$y;
    }
}
// Pass both parameters.
$p1 = new Point(45);
// Pass only the required parameter. $y will take its default value of 0.
$p2 = new Point(4);
// With named parameters (as of PHP 8.0):
$p3 = new Point(y5x4);
?>
7

__construct(int $xint $y 0) {
        
$this->$x;
        
$this->$y;
    }
}
// Pass both parameters.
$p1 = new Point(45);
// Pass only the required parameter. $y will take its default value of 0.
$p2 = new Point(4);
// With named parameters (as of PHP 8.0):
$p3 = new Point(y5x4);
?>
8

__construct(int $xint $y 0) {
        
$this->$x;
        
$this->$y;
    }
}
// Pass both parameters.
$p1 = new Point(45);
// Pass only the required parameter. $y will take its default value of 0.
$p2 = new Point(4);
// With named parameters (as of PHP 8.0):
$p3 = new Point(y5x4);
?>
9

E_DEPRECATED0

class BaseClass {
    function 
__construct() {
        print 
"In BaseClass constructor\n";
    }
}
4

1 tháng trước

Reza Mahjourian ¶

E_DEPRECATED2

E_DEPRECATED3

E_DEPRECATED4

E_DEPRECATED5

E_DEPRECATED6

16 năm trước

instatiendaweb tại gmail dot com ¶

E_DEPRECATED7

E_DEPRECATED8

E_DEPRECATED9

class Decimal
{
    private $value; // constraint: a non-empty string of digits
    private $scale; // constraint: an integer >= 0

    private function __construct($value, $scale = 0)
    {
        // Value and scale are expected to be validated here.
        // Because the constructor is private, it can only be called from within the class,
        // so we can avoid to perform validation at this step, and just trust the caller.

        $this->value = $value;
        $this->scale = $scale;
    }

    public static function zero()
    {
        return new self('0');
    }

    public static function fromString($string)
    {
        // Perform sanity checks on the string, and compute the value & scale

        // ...

        return new self($value, $scale);
    }
}
00

class Decimal
{
    private $value; // constraint: a non-empty string of digits
    private $scale; // constraint: an integer >= 0

    private function __construct($value, $scale = 0)
    {
        // Value and scale are expected to be validated here.
        // Because the constructor is private, it can only be called from within the class,
        // so we can avoid to perform validation at this step, and just trust the caller.

        $this->value = $value;
        $this->scale = $scale;
    }

    public static function zero()
    {
        return new self('0');
    }

    public static function fromString($string)
    {
        // Perform sanity checks on the string, and compute the value & scale

        // ...

        return new self($value, $scale);
    }
}
01

class Decimal
{
    private $value; // constraint: a non-empty string of digits
    private $scale; // constraint: an integer >= 0

    private function __construct($value, $scale = 0)
    {
        // Value and scale are expected to be validated here.
        // Because the constructor is private, it can only be called from within the class,
        // so we can avoid to perform validation at this step, and just trust the caller.

        $this->value = $value;
        $this->scale = $scale;
    }

    public static function zero()
    {
        return new self('0');
    }

    public static function fromString($string)
    {
        // Perform sanity checks on the string, and compute the value & scale

        // ...

        return new self($value, $scale);
    }
}
02

class Decimal
{
    private $value; // constraint: a non-empty string of digits
    private $scale; // constraint: an integer >= 0

    private function __construct($value, $scale = 0)
    {
        // Value and scale are expected to be validated here.
        // Because the constructor is private, it can only be called from within the class,
        // so we can avoid to perform validation at this step, and just trust the caller.

        $this->value = $value;
        $this->scale = $scale;
    }

    public static function zero()
    {
        return new self('0');
    }

    public static function fromString($string)
    {
        // Perform sanity checks on the string, and compute the value & scale

        // ...

        return new self($value, $scale);
    }
}
03

class Decimal
{
    private $value; // constraint: a non-empty string of digits
    private $scale; // constraint: an integer >= 0

    private function __construct($value, $scale = 0)
    {
        // Value and scale are expected to be validated here.
        // Because the constructor is private, it can only be called from within the class,
        // so we can avoid to perform validation at this step, and just trust the caller.

        $this->value = $value;
        $this->scale = $scale;
    }

    public static function zero()
    {
        return new self('0');
    }

    public static function fromString($string)
    {
        // Perform sanity checks on the string, and compute the value & scale

        // ...

        return new self($value, $scale);
    }
}
04

class BaseClass {
    function 
__construct() {
        print 
"In BaseClass constructor\n";
    }
}
4

Các nhà xây dựng nên công khai hay riêng tư?

Không có quy tắc nào mà nhà xây dựng được công khai. Nói chung, chúng tôi xác định nó công khai chỉ vì chúng tôi cũng muốn khởi tạo nó từ các lớp khác. Nhà xây dựng riêng có nghĩa là "Tôi không để bất cứ ai tạo trường hợp của mình ngoại trừ tôi". Vì vậy, thông thường bạn sẽ làm điều này khi bạn muốn có một mẫu singleton. . Generally we define it public just because we would like to instantiate it from other classes too . Private constructor means,"i dont let anyone create my instance except me ". So normally you would do this when you like to have a singleton pattern.

Constructor có thể là PHP riêng không?

Chất xây dựng có thể được làm riêng hoặc được bảo vệ để ngăn chặn nó được gọi là bên ngoài. Nếu vậy, chỉ có một phương pháp tĩnh sẽ có thể khởi tạo lớp. Bởi vì chúng nằm trong cùng một định nghĩa lớp, họ có quyền truy cập vào các phương thức riêng tư, ngay cả khi không có cùng một thể hiện đối tượng.. If so, only a static method will be able to instantiate the class. Because they are in the same class definition they have access to private methods, even if not of the same object instance.

Các nhà xây dựng phải là riêng tư?

Vâng, chúng tôi có thể tuyên bố một hàm tạo là riêng tư.Nếu chúng ta tuyên bố một hàm tạo là riêng tư, chúng ta không thể tạo một đối tượng của một lớp.. If we declare a constructor as private we are not able to create an object of a class.

Khi nào một nhà xây dựng nên được riêng tư?

Các hàm tạo riêng được sử dụng để ngăn chặn việc tạo các trường hợp của một lớp khi không có trường hoặc phương thức thể hiện, chẳng hạn như lớp toán học hoặc khi một phương thức được gọi để có được một thể hiện của một lớp.Nếu tất cả các phương pháp trong lớp là tĩnh, hãy xem xét việc làm cho lớp hoàn chỉnh tĩnh.when there are no instance fields or methods, such as the Math class, or when a method is called to obtain an instance of a class. If all the methods in the class are static, consider making the complete class static.