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

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

9

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

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

Chủ Đề