理解受保护访问说明符的概念 [英] Understanding the concept of protected access specifier

查看:120
本文介绍了理解受保护访问说明符的概念的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我是编码的新手。我正在播放受保护的访问说明符。当一个变量或函数被声明为protected时,它变为公共的当前类及其子类,并且它对外部世界是私有的。现在在以下程序中

$ obj-> print_name(Virat,Kohli);没有得到执行我收到以下错误:

致命错误:从C:\ xampp \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\第90行的test.php。为什么会这样?

print_name()可以被Parent_class的对象访问,不是吗?



此外,如果我删除受保护的关键字,该功能将变为公共。为什么以下行不起作用?

print_name(Virat,Kohli);

因为它是公开的,所以外面的世界都可以访问't?



Hello I am new to coding. I was playing protected access specifier. When a variable or function is declared as protected it becomes public to the current class as well as its child class and it is private to the outside world. Now in the following program
$obj->print_name("Virat","Kohli"); is not getting executed i am getting the following error:
Fatal error: Call to protected method Parent_class::print_name() from context '' in C:\xampp\htdocs\ABC\test.php on line 90. Why is it so??
print_name() is accessible by the object of Parent_class, isn´t it?

Also if i remove the protected keyword the function becomes public. Why does the following line doesn't work?
print_name("Virat","Kohli");
Since it is public it is accessible to the outside world, isn't ?

class Parent_class
            {
                var $first_name="David";
                var $last_name="Hussey";

                function __construct()
                {
                    echo "Parent class object instantiated" . "</br>";
                }


                protected function print_name($name,$surname)
                {
                    $first_name=$name;
                    $last_name=$surname;

                    echo $first_name . " ". $last_name . "</br>";
                }
            }

            class Child_class extends Parent_class
            {
                var $x="Roger";
                var $y="Nadal";

                function __construct()
                {
                    Parent_class::__construct();
                    echo "Child class object instantiated" . "</br>";
                }

                function call_parent_class_function()
                {
                    Parent_class::print_name($this->x,$this->y);
                }

            }

            $obj = new Child_class;
            $obj->call_parent_class_function();

            $obj1 = new Parent_class;
            $obj->print_name("Virat","Kohli");

            print_name("Virat","Kohli");

推荐答案

obj - > print_name( Virat, 科利);没有得到执行我收到以下错误:

致命错误:从C:\ xampp \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\第90行的test.php。为什么会这样?

print_name()可以被Parent_class的对象访问,不是吗?



此外,如果我删除受保护的关键字,该功能将变为公共。为什么以下行不起作用?

print_name(Virat,Kohli);

因为它是公开的,所以外面的世界都可以访问't?



obj->print_name("Virat","Kohli"); is not getting executed i am getting the following error:
Fatal error: Call to protected method Parent_class::print_name() from context '' in C:\xampp\htdocs\ABC\test.php on line 90. Why is it so??
print_name() is accessible by the object of Parent_class, isn´t it?

Also if i remove the protected keyword the function becomes public. Why does the following line doesn't work?
print_name("Virat","Kohli");
Since it is public it is accessible to the outside world, isn't ?

class Parent_class
            {
                var


first_name = David;
var
first_name="David"; var


last_name = Hussey;

函数__construct()
{
echo 实例化父类对象 < / br>;
}


protected function print_name(
last_name="Hussey"; function __construct() { echo "Parent class object instantiated" . "</br>"; } protected function print_name(


这篇关于理解受保护访问说明符的概念的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆