为了账号安全,请及时绑定邮箱和手机立即绑定

访问并返回函数中的私有属性

访问并返回函数中的私有属性

PHP
幕布斯6054654 2023-09-08 21:31:17
我观看了有关 PHP 的教程,在他的视频中,他将属性设置为private,这意味着我们无法在class. 但是当他创建一个返回该private属性的函数时,他就能够获得echo该属性。它对他有用,但只是不打印任何东西<?php        class Book {            private $rating;            public $title;            function __construct($title, $rating) {                $this -> title = $title;                $this -> rate = $rate;            }                         function getRating() {                return $this -> rating;            }        }        $book1 = new Book('Harry Potter', 'PG-13'); // object instance        echo $book1 -> getRating(); // Does not print anything?>更新我变了$this -> rate = $rate;到$this -> rate = $rating;但它仍然没有打印任何东西
查看完整描述

2 回答

?
一只甜甜圈

TA贡献1836条经验 获得超5个赞

在你的构造函数中__construct你应该做类似的事情:

$this -> rating = $rating;

不是:

$this -> rate = $rate;


查看完整回答
反对 回复 2023-09-08
?
浮云间

TA贡献1829条经验 获得超4个赞

你从哪里得到$rate变量?基本上没啥地方用。如果$Rating进来,并且$this->Rating是全局变量,那么就没有$Rate变量。


$this->title和 等之间也没有空格。


代码:


<?php

    

    // Use this function to make sure your error handling is tightest:

    error_reporting(E_ALL);

    

    // Start a new class

    class Book {

        

        // We are setting the rating to be private:

        private $rating;

        

        // And we are setting the title to be public: You could also use 'var' here instead:

        var $title;

        

        // This is the function behind new Book () .. it is a construction function.

        function __construct ($title, $rating) {

            

            // You have $title coming and you are setting the classes global variable to it as well:

            $this->title = $title;

            

            // Same as above, but this is private, so outside of this class you cant access it:

            $this->rating = $rating;

        } 

        

        // This the function to get the rating:

        function getRating () {

            

            // This is the variable from the 5th line now. It is in fact private, but since the

            // function is inside the class, then this function getRating is allowed to access the variable

            // there for it will print it out without problems:

            return $this->rating;

        }

    }

    

    // Init the class and insert some basic information:

    $book1 = new Book('Harry Potter', 'PG-13');

    

    // Will print out 'PG-13'

    echo $book1->getRating() . '<br>';

    

    // Title will show up, as it is public:

    echo $book1->title . '<br>';

    

    // But accessing the rating directly, will not show anything:

    echo $book1->rating . '<br>';

    

    // Since the rating is private, then it will ultimate throw an error,

    // so this will kill the script or show the error, depending on your hosting settings:

    echo 'This probably wount show up';

    // yup, it gives you:

    // Fatal error: Uncaught Error: Cannot access private property Book::$rating in [.........]

?>

输出:

https://img1.sycdn.imooc.com//64fb227100019da405560077.jpg

希望这可以帮助您进一步学习更多 PHP。



查看完整回答
反对 回复 2023-09-08
  • 2 回答
  • 0 关注
  • 49 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信