Hướng dẫn public variable in class php - biến công khai trong lớp php

Bạn đang "cài đặt" giá trị của biến/thuộc tính đó. Không ghi đè hoặc quá tải nó. Mã của bạn rất, rất phổ biến và bình thường.

Tất cả các thuật ngữ này ("set", "ghi đè", "quá tải") có ý nghĩa cụ thể. Ghi đè và quá tải là về tính đa hình (phân lớp).

Từ http://en.wikipedia.org/wiki/Object-oriented_programming:

Đa hình cho phép lập trình viên đối xử với các thành viên trong lớp giống như các thành viên của lớp phụ huynh của họ. Chính xác hơn, tính đa hình trong lập trình hướng đối tượng là khả năng của các đối tượng thuộc các loại dữ liệu khác nhau để đáp ứng các cuộc gọi phương thức của cùng tên, mỗi đối tượng theo một hành vi cụ thể loại thích hợp. Một phương pháp hoặc toán tử như +, -, hoặc *, có thể được áp dụng trừu tượng trong nhiều tình huống khác nhau. Nếu một con chó được lệnh nói (), điều này có thể gợi ra một vỏ cây (). Tuy nhiên, nếu một con lợn được lệnh nói (), điều này có thể gợi ra một oink (). Cả hai đều thừa hưởng nói () từ động vật, nhưng các phương pháp lớp dẫn xuất của chúng ghi đè lên các phương thức của lớp cha; Điều này đang vượt qua tính đa hình. Quá tải đa hình là việc sử dụng một chữ ký phương pháp hoặc một toán tử như "+", để thực hiện một số chức năng khác nhau tùy thuộc vào việc thực hiện. Ví dụ, toán tử "+" có thể được sử dụng để thực hiện bổ sung số nguyên, bổ sung float, kết hợp danh sách hoặc nối chuỗi. Bất kỳ hai lớp con nào của số, chẳng hạn như số nguyên và gấp đôi, dự kiến ​​sẽ cộng lại đúng cách trong ngôn ngữ OOP. Do đó, ngôn ngữ phải làm quá tải toán tử bổ sung, "+", để hoạt động theo cách này. Điều này giúp cải thiện khả năng đọc mã. Làm thế nào điều này được thực hiện thay đổi từ ngôn ngữ này sang ngôn ngữ khác, nhưng hầu hết các ngôn ngữ OOP đều hỗ trợ ít nhất một số mức độ quá tải đa hình.

Tushar Dot Khan0122 tại Gmail Dot Com ¶

3 năm trước

Marce! ¶

Imran tại phptrack dot com

/**
 * Define MyClass
 */
class MyClass
{
    public 
$public 'Public';
    protected 
$protected 'Protected';
    private 
$private 'Private';

    function

printHello()
    {
        echo 
$this->public;
        echo 
$this->protected;
        echo 
$this->private;
    }
}
$obj = new MyClass();
echo 
$obj->public// Works
echo $obj->protected// Fatal Error
echo $obj->private// Fatal Error
$obj->printHello(); // Shows Public, Protected and Private

/**
 * Define MyClass2
 */

class MyClass2 extends MyClass
{
    
// We can redeclare the public and protected properties, but not private
    
public $public 'Public2';
    protected 
$protected 'Protected2';

    function

printHello()
    {
        echo 
$this->public;
        echo 
$this->protected;
        echo 
$this->private;
    }
}
$obj2 = new MyClass2();
echo 
$obj2->public// Works
echo $obj2->protected// Fatal Error
echo $obj2->private// Undefined
$obj2->printHello(); // Shows Public2, Protected2, Undefined?>

Khả năng hiển thị phương pháp

Phương pháp lớp có thể được định nghĩa là công khai, riêng tư hoặc được bảo vệ. Các phương thức được khai báo mà không có bất kỳ từ khóa hiển thị rõ ràng được xác định là công khai.

Ví dụ #2 Tuyên bố Phương pháp

/**
 * Define MyClass
 */
class MyClass
{
    
// Declare a public constructor
    
public function __construct() { }// Declare a public method
    
public function MyPublic() { }// Declare a protected method
    
protected function MyProtected() { }// Declare a private method
    
private function MyPrivate() { }// This is public
    
function Foo()
    {
        
$this->MyPublic();
        
$this->MyProtected();
        
$this->MyPrivate();
    }
}
$myclass = new MyClass;
$myclass->MyPublic(); // Works
$myclass->MyProtected(); // Fatal Error
$myclass->MyPrivate(); // Fatal Error
$myclass->Foo(); // Public, Protected and Private work

/**
 * Define MyClass2
 */

class MyClass2 extends MyClass
{
    
// This is public
    
function Foo2()
    {
        
$this->MyPublic();
        
$this->MyProtected();
        
$this->MyPrivate(); // Fatal Error
    
}
}
$myclass2 = new MyClass2;
$myclass2->MyPublic(); // Works
$myclass2->Foo2(); // Public and Protected work, not Privateclass Bar 
{
    public function 
test() {
        
$this->testPrivate();
        
$this->testPublic();
    }

    public function

testPublic() {
        echo 
"Bar::testPublic\n";
    }

        private function

testPrivate() {
        echo 
"Bar::testPrivate\n";
    }
}

class

Foo extends Bar 
{
    public function 
testPublic() {
        echo 
"Foo::testPublic\n";
    }

        private function

testPrivate() {
        echo 
"Foo::testPrivate\n";
    }
}
$myFoo = new Foo();
$myFoo->test(); // Bar::testPrivate 
                // Foo::testPublic
?>

Khả năng hiển thị liên tục

Kể từ Php 7.1.0, các hằng số lớp có thể được định nghĩa là công khai, riêng tư hoặc được bảo vệ. Các hằng số được khai báo mà không có bất kỳ từ khóa hiển thị rõ ràng nào được xác định là công khai.

Ví dụ #3 Tuyên bố liên tục kể từ Php 7.1.0

public0

public1

public2

Khả năng hiển thị từ các đối tượng khác

Các đối tượng cùng loại sẽ có quyền truy cập vào nhau các thành viên riêng tư và được bảo vệ mặc dù chúng không phải là cùng một trường hợp. Điều này là do các chi tiết cụ thể thực hiện đã được biết khi bên trong các đối tượng đó.

Ví dụ #4 Truy cập các thành viên riêng của cùng loại đối tượng

public3

public1

public5

Ví dụ trên sẽ xuất ra:

string(5) "hello"
Accessed the private method.

WBCarts tại Juno Dot Com ¶

10 năm trước

public6

public7

public8

public9

protected0

protected1

protected2

những gì ở từng chấm com ¶

13 năm trước

protected3

protected4

protected5

protected6

protected7

protected8

protected9

PGL tại Yoyo Dot org ¶

7 năm trước

private0

private1

private2

private3

private4

Stephane tại Harobed Dot org ¶

16 năm trước

private5

private6

private7

private8

Kostya tại Eltexsoft dot com ¶

1 năm trước

private9

/**
 * Define MyClass
 */
class MyClass
{
    public 
$public 'Public';
    protected 
$protected 'Protected';
    private 
$private 'Private';
0

/**
 * Define MyClass
 */
class MyClass
{
    public 
$public 'Public';
    protected 
$protected 'Protected';
    private 
$private 'Private';
1

Alexaulbach tại Mayflower Dot de ¶

10 năm trước

/**
 * Define MyClass
 */
class MyClass
{
    public 
$public 'Public';
    protected 
$protected 'Protected';
    private 
$private 'Private';
2

/**
 * Define MyClass
 */
class MyClass
{
    public 
$public 'Public';
    protected 
$protected 'Protected';
    private 
$private 'Private';
3

/**
 * Define MyClass
 */
class MyClass
{
    public 
$public 'Public';
    protected 
$protected 'Protected';
    private 
$private 'Private';
4

những gì ở từng chấm com ¶

16 năm trước

/**
 * Define MyClass
 */
class MyClass
{
    public 
$public 'Public';
    protected 
$protected 'Protected';
    private 
$private 'Private';
5

/**
 * Define MyClass
 */
class MyClass
{
    public 
$public 'Public';
    protected 
$protected 'Protected';
    private 
$private 'Private';
6

/**
 * Define MyClass
 */
class MyClass
{
    public 
$public 'Public';
    protected 
$protected 'Protected';
    private 
$private 'Private';
7

/**
 * Define MyClass
 */
class MyClass
{
    public 
$public 'Public';
    protected 
$protected 'Protected';
    private 
$private 'Private';
8

Kostya tại Eltexsoft dot com ¶

1 năm trước

/**
 * Define MyClass
 */
class MyClass
{
    public 
$public 'Public';
    protected 
$protected 'Protected';
    private 
$private 'Private';
9

    function0

    function1

    function2

    function3

    function4

Alexaulbach tại Mayflower Dot de ¶

13 năm trước

    function5

    function6

    function7

    function8

    function9

PGL tại Yoyo Dot org ¶

13 năm trước

printHello()
    {
        echo 
$this->public;
        echo 
$this->protected;
        echo 
$this->private;
    }
}
$obj = new MyClass();
echo 
$obj->public// Works
echo $obj->protected// Fatal Error
echo $obj->private// Fatal Error
$obj->printHello(); // Shows Public, Protected and Private

/**
 * Define MyClass2
 */

class MyClass2 extends MyClass
{
    
// We can redeclare the public and protected properties, but not private
    
public $public 'Public2';
    protected 
$protected 'Protected2';

    function

printHello()
    {
        echo 
$this->public;
        echo 
$this->protected;
        echo 
$this->private;
    }
}
$obj2 = new MyClass2();
echo 
$obj2->public// Works
echo $obj2->protected// Fatal Error
echo $obj2->private// Undefined
$obj2->printHello(); // Shows Public2, Protected2, Undefined?>
0

printHello()
    {
        echo 
$this->public;
        echo 
$this->protected;
        echo 
$this->private;
    }
}
$obj = new MyClass();
echo 
$obj->public// Works
echo $obj->protected// Fatal Error
echo $obj->private// Fatal Error
$obj->printHello(); // Shows Public, Protected and Private

/**
 * Define MyClass2
 */

class MyClass2 extends MyClass
{
    
// We can redeclare the public and protected properties, but not private
    
public $public 'Public2';
    protected 
$protected 'Protected2';

    function

printHello()
    {
        echo 
$this->public;
        echo 
$this->protected;
        echo 
$this->private;
    }
}
$obj2 = new MyClass2();
echo 
$obj2->public// Works
echo $obj2->protected// Fatal Error
echo $obj2->private// Undefined
$obj2->printHello(); // Shows Public2, Protected2, Undefined?>
1

printHello()
    {
        echo 
$this->public;
        echo 
$this->protected;
        echo 
$this->private;
    }
}
$obj = new MyClass();
echo 
$obj->public// Works
echo $obj->protected// Fatal Error
echo $obj->private// Fatal Error
$obj->printHello(); // Shows Public, Protected and Private

/**
 * Define MyClass2
 */

class MyClass2 extends MyClass
{
    
// We can redeclare the public and protected properties, but not private
    
public $public 'Public2';
    protected 
$protected 'Protected2';

    function

printHello()
    {
        echo 
$this->public;
        echo 
$this->protected;
        echo 
$this->private;
    }
}
$obj2 = new MyClass2();
echo 
$obj2->public// Works
echo $obj2->protected// Fatal Error
echo $obj2->private// Undefined
$obj2->printHello(); // Shows Public2, Protected2, Undefined?>
2

printHello()
    {
        echo 
$this->public;
        echo 
$this->protected;
        echo 
$this->private;
    }
}
$obj = new MyClass();
echo 
$obj->public// Works
echo $obj->protected// Fatal Error
echo $obj->private// Fatal Error
$obj->printHello(); // Shows Public, Protected and Private

/**
 * Define MyClass2
 */

class MyClass2 extends MyClass
{
    
// We can redeclare the public and protected properties, but not private
    
public $public 'Public2';
    protected 
$protected 'Protected2';

    function

printHello()
    {
        echo 
$this->public;
        echo 
$this->protected;
        echo 
$this->private;
    }
}
$obj2 = new MyClass2();
echo 
$obj2->public// Works
echo $obj2->protected// Fatal Error
echo $obj2->private// Undefined
$obj2->printHello(); // Shows Public2, Protected2, Undefined?>
3

printHello()
    {
        echo 
$this->public;
        echo 
$this->protected;
        echo 
$this->private;
    }
}
$obj = new MyClass();
echo 
$obj->public// Works
echo $obj->protected// Fatal Error
echo $obj->private// Fatal Error
$obj->printHello(); // Shows Public, Protected and Private

/**
 * Define MyClass2
 */

class MyClass2 extends MyClass
{
    
// We can redeclare the public and protected properties, but not private
    
public $public 'Public2';
    protected 
$protected 'Protected2';

    function

printHello()
    {
        echo 
$this->public;
        echo 
$this->protected;
        echo 
$this->private;
    }
}
$obj2 = new MyClass2();
echo 
$obj2->public// Works
echo $obj2->protected// Fatal Error
echo $obj2->private// Undefined
$obj2->printHello(); // Shows Public2, Protected2, Undefined?>
4

printHello()
    {
        echo 
$this->public;
        echo 
$this->protected;
        echo 
$this->private;
    }
}
$obj = new MyClass();
echo 
$obj->public// Works
echo $obj->protected// Fatal Error
echo $obj->private// Fatal Error
$obj->printHello(); // Shows Public, Protected and Private

/**
 * Define MyClass2
 */

class MyClass2 extends MyClass
{
    
// We can redeclare the public and protected properties, but not private
    
public $public 'Public2';
    protected 
$protected 'Protected2';

    function

printHello()
    {
        echo 
$this->public;
        echo 
$this->protected;
        echo 
$this->private;
    }
}
$obj2 = new MyClass2();
echo 
$obj2->public// Works
echo $obj2->protected// Fatal Error
echo $obj2->private// Undefined
$obj2->printHello(); // Shows Public2, Protected2, Undefined?>
5

printHello()
    {
        echo 
$this->public;
        echo 
$this->protected;
        echo 
$this->private;
    }
}
$obj = new MyClass();
echo 
$obj->public// Works
echo $obj->protected// Fatal Error
echo $obj->private// Fatal Error
$obj->printHello(); // Shows Public, Protected and Private

/**
 * Define MyClass2
 */

class MyClass2 extends MyClass
{
    
// We can redeclare the public and protected properties, but not private
    
public $public 'Public2';
    protected 
$protected 'Protected2';

    function

printHello()
    {
        echo 
$this->public;
        echo 
$this->protected;
        echo 
$this->private;
    }
}
$obj2 = new MyClass2();
echo 
$obj2->public// Works
echo $obj2->protected// Fatal Error
echo $obj2->private// Undefined
$obj2->printHello(); // Shows Public2, Protected2, Undefined?>
6

printHello()
    {
        echo 
$this->public;
        echo 
$this->protected;
        echo 
$this->private;
    }
}
$obj = new MyClass();
echo 
$obj->public// Works
echo $obj->protected// Fatal Error
echo $obj->private// Fatal Error
$obj->printHello(); // Shows Public, Protected and Private

/**
 * Define MyClass2
 */

class MyClass2 extends MyClass
{
    
// We can redeclare the public and protected properties, but not private
    
public $public 'Public2';
    protected 
$protected 'Protected2';

    function

printHello()
    {
        echo 
$this->public;
        echo 
$this->protected;
        echo 
$this->private;
    }
}
$obj2 = new MyClass2();
echo 
$obj2->public// Works
echo $obj2->protected// Fatal Error
echo $obj2->private// Undefined
$obj2->printHello(); // Shows Public2, Protected2, Undefined?>
7

printHello()
    {
        echo 
$this->public;
        echo 
$this->protected;
        echo 
$this->private;
    }
}
$obj = new MyClass();
echo 
$obj->public// Works
echo $obj->protected// Fatal Error
echo $obj->private// Fatal Error
$obj->printHello(); // Shows Public, Protected and Private

/**
 * Define MyClass2
 */

class MyClass2 extends MyClass
{
    
// We can redeclare the public and protected properties, but not private
    
public $public 'Public2';
    protected 
$protected 'Protected2';

    function

printHello()
    {
        echo 
$this->public;
        echo 
$this->protected;
        echo 
$this->private;
    }
}
$obj2 = new MyClass2();
echo 
$obj2->public// Works
echo $obj2->protected// Fatal Error
echo $obj2->private// Undefined
$obj2->printHello(); // Shows Public2, Protected2, Undefined?>
8

printHello()
    {
        echo 
$this->public;
        echo 
$this->protected;
        echo 
$this->private;
    }
}
$obj = new MyClass();
echo 
$obj->public// Works
echo $obj->protected// Fatal Error
echo $obj->private// Fatal Error
$obj->printHello(); // Shows Public, Protected and Private

/**
 * Define MyClass2
 */

class MyClass2 extends MyClass
{
    
// We can redeclare the public and protected properties, but not private
    
public $public 'Public2';
    protected 
$protected 'Protected2';

    function

printHello()
    {
        echo 
$this->public;
        echo 
$this->protected;
        echo 
$this->private;
    }
}
$obj2 = new MyClass2();
echo 
$obj2->public// Works
echo $obj2->protected// Fatal Error
echo $obj2->private// Undefined
$obj2->printHello(); // Shows Public2, Protected2, Undefined?>
9

PGL tại Yoyo Dot org ¶

10 năm trước

/**
 * Define MyClass
 */
class MyClass
{
    
// Declare a public constructor
    
public function __construct() { }// Declare a public method
    
public function MyPublic() { }// Declare a protected method
    
protected function MyProtected() { }// Declare a private method
    
private function MyPrivate() { }// This is public
    
function Foo()
    {
        
$this->MyPublic();
        
$this->MyProtected();
        
$this->MyPrivate();
    }
}
$myclass = new MyClass;
$myclass->MyPublic(); // Works
$myclass->MyProtected(); // Fatal Error
$myclass->MyPrivate(); // Fatal Error
$myclass->Foo(); // Public, Protected and Private work
0

/**
 * Define MyClass
 */
class MyClass
{
    
// Declare a public constructor
    
public function __construct() { }// Declare a public method
    
public function MyPublic() { }// Declare a protected method
    
protected function MyProtected() { }// Declare a private method
    
private function MyPrivate() { }// This is public
    
function Foo()
    {
        
$this->MyPublic();
        
$this->MyProtected();
        
$this->MyPrivate();
    }
}
$myclass = new MyClass;
$myclass->MyPublic(); // Works
$myclass->MyProtected(); // Fatal Error
$myclass->MyPrivate(); // Fatal Error
$myclass->Foo(); // Public, Protected and Private work
1

/**
 * Define MyClass
 */
class MyClass
{
    
// Declare a public constructor
    
public function __construct() { }// Declare a public method
    
public function MyPublic() { }// Declare a protected method
    
protected function MyProtected() { }// Declare a private method
    
private function MyPrivate() { }// This is public
    
function Foo()
    {
        
$this->MyPublic();
        
$this->MyProtected();
        
$this->MyPrivate();
    }
}
$myclass = new MyClass;
$myclass->MyPublic(); // Works
$myclass->MyProtected(); // Fatal Error
$myclass->MyPrivate(); // Fatal Error
$myclass->Foo(); // Public, Protected and Private work
2

những gì ở từng chấm com ¶

10 năm trước

/**
 * Define MyClass
 */
class MyClass
{
    
// Declare a public constructor
    
public function __construct() { }// Declare a public method
    
public function MyPublic() { }// Declare a protected method
    
protected function MyProtected() { }// Declare a private method
    
private function MyPrivate() { }// This is public
    
function Foo()
    {
        
$this->MyPublic();
        
$this->MyProtected();
        
$this->MyPrivate();
    }
}
$myclass = new MyClass;
$myclass->MyPublic(); // Works
$myclass->MyProtected(); // Fatal Error
$myclass->MyPrivate(); // Fatal Error
$myclass->Foo(); // Public, Protected and Private work
3

/**
 * Define MyClass
 */
class MyClass
{
    
// Declare a public constructor
    
public function __construct() { }// Declare a public method
    
public function MyPublic() { }// Declare a protected method
    
protected function MyProtected() { }// Declare a private method
    
private function MyPrivate() { }// This is public
    
function Foo()
    {
        
$this->MyPublic();
        
$this->MyProtected();
        
$this->MyPrivate();
    }
}
$myclass = new MyClass;
$myclass->MyPublic(); // Works
$myclass->MyProtected(); // Fatal Error
$myclass->MyPrivate(); // Fatal Error
$myclass->Foo(); // Public, Protected and Private work
4

/**
 * Define MyClass
 */
class MyClass
{
    
// Declare a public constructor
    
public function __construct() { }// Declare a public method
    
public function MyPublic() { }// Declare a protected method
    
protected function MyProtected() { }// Declare a private method
    
private function MyPrivate() { }// This is public
    
function Foo()
    {
        
$this->MyPublic();
        
$this->MyProtected();
        
$this->MyPrivate();
    }
}
$myclass = new MyClass;
$myclass->MyPublic(); // Works
$myclass->MyProtected(); // Fatal Error
$myclass->MyPrivate(); // Fatal Error
$myclass->Foo(); // Public, Protected and Private work
5

/**
 * Define MyClass
 */
class MyClass
{
    public 
$public 'Public';
    protected 
$protected 'Protected';
    private 
$private 'Private';
7

/**
 * Define MyClass
 */
class MyClass
{
    
// Declare a public constructor
    
public function __construct() { }// Declare a public method
    
public function MyPublic() { }// Declare a protected method
    
protected function MyProtected() { }// Declare a private method
    
private function MyPrivate() { }// This is public
    
function Foo()
    {
        
$this->MyPublic();
        
$this->MyProtected();
        
$this->MyPrivate();
    }
}
$myclass = new MyClass;
$myclass->MyPublic(); // Works
$myclass->MyProtected(); // Fatal Error
$myclass->MyPrivate(); // Fatal Error
$myclass->Foo(); // Public, Protected and Private work
7

những gì ở từng chấm com ¶

10 năm trước

/**
 * Define MyClass
 */
class MyClass
{
    
// Declare a public constructor
    
public function __construct() { }// Declare a public method
    
public function MyPublic() { }// Declare a protected method
    
protected function MyProtected() { }// Declare a private method
    
private function MyPrivate() { }// This is public
    
function Foo()
    {
        
$this->MyPublic();
        
$this->MyProtected();
        
$this->MyPrivate();
    }
}
$myclass = new MyClass;
$myclass->MyPublic(); // Works
$myclass->MyProtected(); // Fatal Error
$myclass->MyPrivate(); // Fatal Error
$myclass->Foo(); // Public, Protected and Private work
8

/**
 * Define MyClass
 */
class MyClass
{
    
// Declare a public constructor
    
public function __construct() { }// Declare a public method
    
public function MyPublic() { }// Declare a protected method
    
protected function MyProtected() { }// Declare a private method
    
private function MyPrivate() { }// This is public
    
function Foo()
    {
        
$this->MyPublic();
        
$this->MyProtected();
        
$this->MyPrivate();
    }
}
$myclass = new MyClass;
$myclass->MyPublic(); // Works
$myclass->MyProtected(); // Fatal Error
$myclass->MyPrivate(); // Fatal Error
$myclass->Foo(); // Public, Protected and Private work
9

/**
 * Define MyClass2
 */
0

những gì ở từng chấm com ¶

13 năm trước

/**
 * Define MyClass2
 */
1

/**
 * Define MyClass2
 */
2

/**
 * Define MyClass2
 */
3

/**
 * Define MyClass2
 */
4

/**
 * Define MyClass2
 */
5

/**
 * Define MyClass2
 */
6

/**
 * Define MyClass2
 */
7

/**
 * Define MyClass2
 */
8

PGL tại Yoyo Dot org ¶

7 năm trước

/**
 * Define MyClass2
 */
9

class MyClass2 extends MyClass
{
    
// This is public
    
function Foo2()
    {
        
$this->MyPublic();
        
$this->MyProtected();
        
$this->MyPrivate(); // Fatal Error
    
}
}
$myclass2 = new MyClass2;
$myclass2->MyPublic(); // Works
$myclass2->Foo2(); // Public and Protected work, not Privateclass Bar 
{
    public function 
test() {
        
$this->testPrivate();
        
$this->testPublic();
    }

    public function

testPublic() {
        echo 
"Bar::testPublic\n";
    }

        private function

testPrivate() {
        echo 
"Bar::testPrivate\n";
    }
}

class

Foo extends Bar 
{
    public function 
testPublic() {
        echo 
"Foo::testPublic\n";
    }

        private function

testPrivate() {
        echo 
"Foo::testPrivate\n";
    }
}
$myFoo = new Foo();
$myFoo->test(); // Bar::testPrivate 
                // Foo::testPublic
?>
0

class MyClass2 extends MyClass
{
    
// This is public
    
function Foo2()
    {
        
$this->MyPublic();
        
$this->MyProtected();
        
$this->MyPrivate(); // Fatal Error
    
}
}
$myclass2 = new MyClass2;
$myclass2->MyPublic(); // Works
$myclass2->Foo2(); // Public and Protected work, not Privateclass Bar 
{
    public function 
test() {
        
$this->testPrivate();
        
$this->testPublic();
    }

    public function

testPublic() {
        echo 
"Bar::testPublic\n";
    }

        private function

testPrivate() {
        echo 
"Bar::testPrivate\n";
    }
}

class

Foo extends Bar 
{
    public function 
testPublic() {
        echo 
"Foo::testPublic\n";
    }

        private function

testPrivate() {
        echo 
"Foo::testPrivate\n";
    }
}
$myFoo = new Foo();
$myFoo->test(); // Bar::testPrivate 
                // Foo::testPublic
?>
1

class MyClass2 extends MyClass
{
    
// This is public
    
function Foo2()
    {
        
$this->MyPublic();
        
$this->MyProtected();
        
$this->MyPrivate(); // Fatal Error
    
}
}
$myclass2 = new MyClass2;
$myclass2->MyPublic(); // Works
$myclass2->Foo2(); // Public and Protected work, not Privateclass Bar 
{
    public function 
test() {
        
$this->testPrivate();
        
$this->testPublic();
    }

    public function

testPublic() {
        echo 
"Bar::testPublic\n";
    }

        private function

testPrivate() {
        echo 
"Bar::testPrivate\n";
    }
}

class

Foo extends Bar 
{
    public function 
testPublic() {
        echo 
"Foo::testPublic\n";
    }

        private function

testPrivate() {
        echo 
"Foo::testPrivate\n";
    }
}
$myFoo = new Foo();
$myFoo->test(); // Bar::testPrivate 
                // Foo::testPublic
?>
2

class MyClass2 extends MyClass
{
    
// This is public
    
function Foo2()
    {
        
$this->MyPublic();
        
$this->MyProtected();
        
$this->MyPrivate(); // Fatal Error
    
}
}
$myclass2 = new MyClass2;
$myclass2->MyPublic(); // Works
$myclass2->Foo2(); // Public and Protected work, not Privateclass Bar 
{
    public function 
test() {
        
$this->testPrivate();
        
$this->testPublic();
    }

    public function

testPublic() {
        echo 
"Bar::testPublic\n";
    }

        private function

testPrivate() {
        echo 
"Bar::testPrivate\n";
    }
}

class

Foo extends Bar 
{
    public function 
testPublic() {
        echo 
"Foo::testPublic\n";
    }

        private function

testPrivate() {
        echo 
"Foo::testPrivate\n";
    }
}
$myFoo = new Foo();
$myFoo->test(); // Bar::testPrivate 
                // Foo::testPublic
?>
3

Stephane tại Harobed Dot org ¶

16 năm trước

class MyClass2 extends MyClass
{
    
// This is public
    
function Foo2()
    {
        
$this->MyPublic();
        
$this->MyProtected();
        
$this->MyPrivate(); // Fatal Error
    
}
}
$myclass2 = new MyClass2;
$myclass2->MyPublic(); // Works
$myclass2->Foo2(); // Public and Protected work, not Privateclass Bar 
{
    public function 
test() {
        
$this->testPrivate();
        
$this->testPublic();
    }

    public function

testPublic() {
        echo 
"Bar::testPublic\n";
    }

        private function

testPrivate() {
        echo 
"Bar::testPrivate\n";
    }
}

class

Foo extends Bar 
{
    public function 
testPublic() {
        echo 
"Foo::testPublic\n";
    }

        private function

testPrivate() {
        echo 
"Foo::testPrivate\n";
    }
}
$myFoo = new Foo();
$myFoo->test(); // Bar::testPrivate 
                // Foo::testPublic
?>
4

class MyClass2 extends MyClass
{
    
// This is public
    
function Foo2()
    {
        
$this->MyPublic();
        
$this->MyProtected();
        
$this->MyPrivate(); // Fatal Error
    
}
}
$myclass2 = new MyClass2;
$myclass2->MyPublic(); // Works
$myclass2->Foo2(); // Public and Protected work, not Privateclass Bar 
{
    public function 
test() {
        
$this->testPrivate();
        
$this->testPublic();
    }

    public function

testPublic() {
        echo 
"Bar::testPublic\n";
    }

        private function

testPrivate() {
        echo 
"Bar::testPrivate\n";
    }
}

class

Foo extends Bar 
{
    public function 
testPublic() {
        echo 
"Foo::testPublic\n";
    }

        private function

testPrivate() {
        echo 
"Foo::testPrivate\n";
    }
}
$myFoo = new Foo();
$myFoo->test(); // Bar::testPrivate 
                // Foo::testPublic
?>
5

class MyClass2 extends MyClass
{
    
// This is public
    
function Foo2()
    {
        
$this->MyPublic();
        
$this->MyProtected();
        
$this->MyPrivate(); // Fatal Error
    
}
}
$myclass2 = new MyClass2;
$myclass2->MyPublic(); // Works
$myclass2->Foo2(); // Public and Protected work, not Privateclass Bar 
{
    public function 
test() {
        
$this->testPrivate();
        
$this->testPublic();
    }

    public function

testPublic() {
        echo 
"Bar::testPublic\n";
    }

        private function

testPrivate() {
        echo 
"Bar::testPrivate\n";
    }
}

class

Foo extends Bar 
{
    public function 
testPublic() {
        echo 
"Foo::testPublic\n";
    }

        private function

testPrivate() {
        echo 
"Foo::testPrivate\n";
    }
}
$myFoo = new Foo();
$myFoo->test(); // Bar::testPrivate 
                // Foo::testPublic
?>
6

class MyClass2 extends MyClass
{
    
// This is public
    
function Foo2()
    {
        
$this->MyPublic();
        
$this->MyProtected();
        
$this->MyPrivate(); // Fatal Error
    
}
}
$myclass2 = new MyClass2;
$myclass2->MyPublic(); // Works
$myclass2->Foo2(); // Public and Protected work, not Privateclass Bar 
{
    public function 
test() {
        
$this->testPrivate();
        
$this->testPublic();
    }

    public function

testPublic() {
        echo 
"Bar::testPublic\n";
    }

        private function

testPrivate() {
        echo 
"Bar::testPrivate\n";
    }
}

class

Foo extends Bar 
{
    public function 
testPublic() {
        echo 
"Foo::testPublic\n";
    }

        private function

testPrivate() {
        echo 
"Foo::testPrivate\n";
    }
}
$myFoo = new Foo();
$myFoo->test(); // Bar::testPrivate 
                // Foo::testPublic
?>
7

Kostya tại Eltexsoft dot com ¶

1 năm trước

class MyClass2 extends MyClass
{
    
// This is public
    
function Foo2()
    {
        
$this->MyPublic();
        
$this->MyProtected();
        
$this->MyPrivate(); // Fatal Error
    
}
}
$myclass2 = new MyClass2;
$myclass2->MyPublic(); // Works
$myclass2->Foo2(); // Public and Protected work, not Privateclass Bar 
{
    public function 
test() {
        
$this->testPrivate();
        
$this->testPublic();
    }

    public function

testPublic() {
        echo 
"Bar::testPublic\n";
    }

        private function

testPrivate() {
        echo 
"Bar::testPrivate\n";
    }
}

class

Foo extends Bar 
{
    public function 
testPublic() {
        echo 
"Foo::testPublic\n";
    }

        private function

testPrivate() {
        echo 
"Foo::testPrivate\n";
    }
}
$myFoo = new Foo();
$myFoo->test(); // Bar::testPrivate 
                // Foo::testPublic
?>
8

class MyClass2 extends MyClass
{
    
// This is public
    
function Foo2()
    {
        
$this->MyPublic();
        
$this->MyProtected();
        
$this->MyPrivate(); // Fatal Error
    
}
}
$myclass2 = new MyClass2;
$myclass2->MyPublic(); // Works
$myclass2->Foo2(); // Public and Protected work, not Privateclass Bar 
{
    public function 
test() {
        
$this->testPrivate();
        
$this->testPublic();
    }

    public function

testPublic() {
        echo 
"Bar::testPublic\n";
    }

        private function

testPrivate() {
        echo 
"Bar::testPrivate\n";
    }
}

class

Foo extends Bar 
{
    public function 
testPublic() {
        echo 
"Foo::testPublic\n";
    }

        private function

testPrivate() {
        echo 
"Foo::testPrivate\n";
    }
}
$myFoo = new Foo();
$myFoo->test(); // Bar::testPrivate 
                // Foo::testPublic
?>
9

public00

public01

Alexaulbach tại Mayflower Dot de ¶

1 năm trước

public02

    function8

public04

Alexaulbach tại Mayflower Dot de ¶

r dot wilczek tại web-appz dot de ¶

public05

public06

public07

public08

Tushar Dot Khan0122 tại Gmail Dot Com ¶

16 năm trước

public09

class MyClass2 extends MyClass
{
    
// This is public
    
function Foo2()
    {
        
$this->MyPublic();
        
$this->MyProtected();
        
$this->MyPrivate(); // Fatal Error
    
}
}
$myclass2 = new MyClass2;
$myclass2->MyPublic(); // Works
$myclass2->Foo2(); // Public and Protected work, not Privateclass Bar 
{
    public function 
test() {
        
$this->testPrivate();
        
$this->testPublic();
    }

    public function

testPublic() {
        echo 
"Bar::testPublic\n";
    }

        private function

testPrivate() {
        echo 
"Bar::testPrivate\n";
    }
}

class

Foo extends Bar 
{
    public function 
testPublic() {
        echo 
"Foo::testPublic\n";
    }

        private function

testPrivate() {
        echo 
"Foo::testPrivate\n";
    }
}
$myFoo = new Foo();
$myFoo->test(); // Bar::testPrivate 
                // Foo::testPublic
?>
6

public11

Kostya tại Eltexsoft dot com ¶

1 năm trước

public12

public13

public14

public15

public16

public17

public18

public19

public20

public21

printHello()
    {
        echo 
$this->public;
        echo 
$this->protected;
        echo 
$this->private;
    }
}
$obj = new MyClass();
echo 
$obj->public// Works
echo $obj->protected// Fatal Error
echo $obj->private// Fatal Error
$obj->printHello(); // Shows Public, Protected and Private

/**
 * Define MyClass2
 */

class MyClass2 extends MyClass
{
    
// We can redeclare the public and protected properties, but not private
    
public $public 'Public2';
    protected 
$protected 'Protected2';

    function

printHello()
    {
        echo 
$this->public;
        echo 
$this->protected;
        echo 
$this->private;
    }
}
$obj2 = new MyClass2();
echo 
$obj2->public// Works
echo $obj2->protected// Fatal Error
echo $obj2->private// Undefined
$obj2->printHello(); // Shows Public2, Protected2, Undefined?>
9

Alexaulbach tại Mayflower Dot de ¶

r dot wilczek tại web-appz dot de ¶

public23

public24

/**
 * Define MyClass
 */
class MyClass
{
    
// Declare a public constructor
    
public function __construct() { }// Declare a public method
    
public function MyPublic() { }// Declare a protected method
    
protected function MyProtected() { }// Declare a private method
    
private function MyPrivate() { }// This is public
    
function Foo()
    {
        
$this->MyPublic();
        
$this->MyProtected();
        
$this->MyPrivate();
    }
}
$myclass = new MyClass;
$myclass->MyPublic(); // Works
$myclass->MyProtected(); // Fatal Error
$myclass->MyPrivate(); // Fatal Error
$myclass->Foo(); // Public, Protected and Private work
9

public26

Tushar Dot Khan0122 tại Gmail Dot Com ¶

13 năm trước

public27

public28

public29

public30

public31

public32

PGL tại Yoyo Dot org ¶

16 năm trước

public33

public34

class MyClass2 extends MyClass
{
    
// This is public
    
function Foo2()
    {
        
$this->MyPublic();
        
$this->MyProtected();
        
$this->MyPrivate(); // Fatal Error
    
}
}
$myclass2 = new MyClass2;
$myclass2->MyPublic(); // Works
$myclass2->Foo2(); // Public and Protected work, not Privateclass Bar 
{
    public function 
test() {
        
$this->testPrivate();
        
$this->testPublic();
    }

    public function

testPublic() {
        echo 
"Bar::testPublic\n";
    }

        private function

testPrivate() {
        echo 
"Bar::testPrivate\n";
    }
}

class

Foo extends Bar 
{
    public function 
testPublic() {
        echo 
"Foo::testPublic\n";
    }

        private function

testPrivate() {
        echo 
"Foo::testPrivate\n";
    }
}
$myFoo = new Foo();
$myFoo->test(); // Bar::testPrivate 
                // Foo::testPublic
?>
2

public36

Kostya tại Eltexsoft dot com ¶

13 năm trước

public37

public38

public39

public40

public41

public42

public43

public44

printHello()
    {
        echo 
$this->public;
        echo 
$this->protected;
        echo 
$this->private;
    }
}
$obj = new MyClass();
echo 
$obj->public// Works
echo $obj->protected// Fatal Error
echo $obj->private// Fatal Error
$obj->printHello(); // Shows Public, Protected and Private

/**
 * Define MyClass2
 */

class MyClass2 extends MyClass
{
    
// We can redeclare the public and protected properties, but not private
    
public $public 'Public2';
    protected 
$protected 'Protected2';

    function

printHello()
    {
        echo 
$this->public;
        echo 
$this->protected;
        echo 
$this->private;
    }
}
$obj2 = new MyClass2();
echo 
$obj2->public// Works
echo $obj2->protected// Fatal Error
echo $obj2->private// Undefined
$obj2->printHello(); // Shows Public2, Protected2, Undefined?>
9

PGL tại Yoyo Dot org ¶

7 năm trước

public46

public47

public48

public49

public50

public51

public52

public53

public54

public55

public56

public57

public58

public59

printHello()
    {
        echo 
$this->public;
        echo 
$this->protected;
        echo 
$this->private;
    }
}
$obj = new MyClass();
echo 
$obj->public// Works
echo $obj->protected// Fatal Error
echo $obj->private// Fatal Error
$obj->printHello(); // Shows Public, Protected and Private

/**
 * Define MyClass2
 */

class MyClass2 extends MyClass
{
    
// We can redeclare the public and protected properties, but not private
    
public $public 'Public2';
    protected 
$protected 'Protected2';

    function

printHello()
    {
        echo 
$this->public;
        echo 
$this->protected;
        echo 
$this->private;
    }
}
$obj2 = new MyClass2();
echo 
$obj2->public// Works
echo $obj2->protected// Fatal Error
echo $obj2->private// Undefined
$obj2->printHello(); // Shows Public2, Protected2, Undefined?>
9

Stephane tại Harobed Dot org ¶

13 năm trước

public61

public62

printHello()
    {
        echo 
$this->public;
        echo 
$this->protected;
        echo 
$this->private;
    }
}
$obj = new MyClass();
echo 
$obj->public// Works
echo $obj->protected// Fatal Error
echo $obj->private// Fatal Error
$obj->printHello(); // Shows Public, Protected and Private

/**
 * Define MyClass2
 */

class MyClass2 extends MyClass
{
    
// We can redeclare the public and protected properties, but not private
    
public $public 'Public2';
    protected 
$protected 'Protected2';

    function

printHello()
    {
        echo 
$this->public;
        echo 
$this->protected;
        echo 
$this->private;
    }
}
$obj2 = new MyClass2();
echo 
$obj2->public// Works
echo $obj2->protected// Fatal Error
echo $obj2->private// Undefined
$obj2->printHello(); // Shows Public2, Protected2, Undefined?>
9

Các biến công khai trong lớp là gì?

Các biến công khai, là các biến có thể nhìn thấy cho tất cả các lớp. Các biến riêng tư, là các biến chỉ hiển thị cho lớp mà chúng thuộc về. Các biến được bảo vệ, là các biến chỉ hiển thị cho lớp mà chúng thuộc về và bất kỳ lớp con nào.variables that are visible to all classes. Private variables, are variables that are visible only to the class to which they belong. Protected variables, are variables that are visible only to the class to which they belong, and any subclasses.

Tại sao không được khuyến nghị làm cho tất cả một biến lớp công khai trong PHP?

Đối với một điều, tất cả các thuộc tính như vậy trở thành thuộc tính công cộng (và tốt hơn là giữ các thuộc tính riêng tư trong nhiều trường hợp cho mục đích đóng gói).Nó cũng làm cho mã trong lớp không dễ đọc nếu các thuộc tính mới được khai báo ở khắp mọi nơi.It also makes the code in the class much less readable if new properties are being declared all over the place.

Php lớp công cộng là gì?

công khai - tài sản hoặc phương thức có thể được truy cập từ mọi nơi.Đây là mặc định.Được bảo vệ - thuộc tính hoặc phương thức có thể được truy cập trong lớp và bởi các lớp có nguồn gốc từ lớp đó.Riêng tư - Thuộc tính hoặc phương thức chỉ có thể được truy cập trong lớp.the property or method can be accessed from everywhere. This is default. protected - the property or method can be accessed within the class and by classes derived from that class. private - the property or method can ONLY be accessed within the class.

Làm thế nào có thể sử dụng biến toàn cầu bên trong lớp trong PHP?

Truy cập biến toàn cầu bên trong hàm: Các cách để truy cập biến toàn cầu bên trong các chức năng là: sử dụng từ khóa toàn cầu.Sử dụng Array Globals [VAR_NAME]: Nó lưu trữ tất cả các biến toàn cầu trong một mảng có tên $ Globals [var_name].Var_name là tên của biến.Using global keyword. Using array GLOBALS[var_name]: It stores all global variables in an array called $GLOBALS[var_name]. Var_name is the name of the variable.