Hướng dẫn how can use private function in class in php? - làm thế nào có thể sử dụng hàm private trong class trong php?

Khả năng hiển thị của một thuộc tính, một phương thức hoặc (kể từ Php 7.1.0), một hằng số có thể được xác định bằng cách tiền tố khai báo với các từ khóa public, protected hoặc private. Các thành viên lớp tuyên bố công khai có thể được truy cập ở mọi nơi. Các thành viên tuyên bố được bảo vệ chỉ có thể được truy cập trong chính lớp và bằng cách kế thừa và các lớp cha. Các thành viên được tuyên bố là riêng tư chỉ có thể được truy cập bởi lớp xác định thành viên.

Khả năng hiển thị tài sản

Các thuộc tính lớp có thể được định nghĩa là công khai, riêng tư hoặc được bảo vệ. Các thuộc tính đượ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ụ số 1 khai báo tài sản

/**
 * 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 ¶

9 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

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

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 ¶

9 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

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

9 năm trước

public02

    function8

public04

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

Tushar Dot Khan0122 tại Gmail Dot Com ¶

public05

public06

public07

public08

3 năm trước

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 ¶

16 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

Kostya tại Eltexsoft dot com ¶

1 năm trước

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

Alexaulbach tại Mayflower Dot de ¶

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 ¶

1 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

Alexaulbach tại Mayflower Dot de ¶

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 ¶

1 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

Làm thế nào để bạn gọi một chức năng riêng trong lớp?

Chúng ta có thể gọi phương thức riêng của một lớp từ một lớp khác trong Java (được xác định bằng cách sử dụng công cụ sửa đổi truy cập riêng trong Java).Chúng ta có thể làm điều này bằng cách thay đổi hành vi thời gian chạy của lớp bằng cách sử dụng một số phương pháp được xác định trước của Java.Để truy cập phương thức riêng của lớp khác nhau, chúng tôi sẽ sử dụng API phản xạ.changing the runtime behavior of the class by using some predefined methods of Java. For accessing private method of different class we will use Reflection API.

Một lớp học có thể được riêng tư trong PHP không?

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.. Methods declared without any explicit visibility keyword are defined as public.

Các chức năng của lớp có thể được riêng tư không?

Riêng tư: Các thành viên trong lớp được tuyên bố là riêng tư chỉ có thể được truy cập bởi các chức năng bên trong lớp.Chúng không được phép truy cập trực tiếp bởi bất kỳ đối tượng hoặc chức năng nào bên ngoài lớp.Chỉ các chức năng thành viên hoặc các chức năng bạn bè mới được phép truy cập các thành viên dữ liệu riêng tư của một lớp.The class members declared as private can be accessed only by the functions inside the class. They are not allowed to be accessed directly by any object or function outside the class. Only the member functions or the friend functions are allowed to access the private data members of a class.

Hàm riêng PHP là gì?

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 ONLY be accessed within the class.