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

我观看了有关 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;

但它仍然没有打印任何东西


幕布斯6054654
浏览 81回答 2
2回答

一只甜甜圈

在你的构造函数中__construct你应该做类似的事情:$this&nbsp;->&nbsp;rating&nbsp;=&nbsp;$rating;不是:$this&nbsp;->&nbsp;rate&nbsp;=&nbsp;$rate;

浮云间

你从哪里得到$rate变量?基本上没啥地方用。如果$Rating进来,并且$this->Rating是全局变量,那么就没有$Rate变量。$this->title和 等之间也没有空格。代码:<?php&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // Use this function to make sure your error handling is tightest:&nbsp; &nbsp; error_reporting(E_ALL);&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // Start a new class&nbsp; &nbsp; class Book {&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // We are setting the rating to be private:&nbsp; &nbsp; &nbsp; &nbsp; private $rating;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // And we are setting the title to be public: You could also use 'var' here instead:&nbsp; &nbsp; &nbsp; &nbsp; var $title;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // This is the function behind new Book () .. it is a construction function.&nbsp; &nbsp; &nbsp; &nbsp; function __construct ($title, $rating) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // You have $title coming and you are setting the classes global variable to it as well:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->title = $title;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Same as above, but this is private, so outside of this class you cant access it:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->rating = $rating;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // This the function to get the rating:&nbsp; &nbsp; &nbsp; &nbsp; function getRating () {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // This is the variable from the 5th line now. It is in fact private, but since the&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // function is inside the class, then this function getRating is allowed to access the variable&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // there for it will print it out without problems:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $this->rating;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // Init the class and insert some basic information:&nbsp; &nbsp; $book1 = new Book('Harry Potter', 'PG-13');&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // Will print out 'PG-13'&nbsp; &nbsp; echo $book1->getRating() . '<br>';&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // Title will show up, as it is public:&nbsp; &nbsp; echo $book1->title . '<br>';&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // But accessing the rating directly, will not show anything:&nbsp; &nbsp; echo $book1->rating . '<br>';&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // Since the rating is private, then it will ultimate throw an error,&nbsp; &nbsp; // so this will kill the script or show the error, depending on your hosting settings:&nbsp; &nbsp; echo 'This probably wount show up';&nbsp; &nbsp; // yup, it gives you:&nbsp; &nbsp; // Fatal error: Uncaught Error: Cannot access private property Book::$rating in [.........]?>输出:希望这可以帮助您进一步学习更多 PHP。
打开App,查看更多内容
随时随地看视频慕课网APP