第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

訪問(wèn)并返回函數(shù)中的私有屬性

訪問(wèn)并返回函數(shù)中的私有屬性

PHP
幕布斯6054654 2023-09-08 21:31:17
我觀看了有關(guān) PHP 的教程,在他的視頻中,他將屬性設(shè)置為private,這意味著我們無(wú)法在class. 但是當(dāng)他創(chuàng)建一個(gè)返回該private屬性的函數(shù)時(shí),他就能夠獲得echo該屬性。它對(duì)他有用,但只是不打印任何東西<?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;但它仍然沒(méi)有打印任何東西
查看完整描述

2 回答

?
一只甜甜圈

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超5個(gè)贊

在你的構(gòu)造函數(shù)中__construct你應(yīng)該做類似的事情:

$this -> rating = $rating;

不是:

$this -> rate = $rate;


查看完整回答
反對(duì) 回復(fù) 2023-09-08
?
浮云間

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超4個(gè)贊

你從哪里得到$rate變量?基本上沒(méi)啥地方用。如果$Rating進(jìn)來(lái),并且$this->Rating是全局變量,那么就沒(méi)有$Rate變量。


$this->title和 等之間也沒(méi)有空格。


代碼:


<?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

希望這可以幫助您進(jìn)一步學(xué)習(xí)更多 PHP。



查看完整回答
反對(duì) 回復(fù) 2023-09-08
  • 2 回答
  • 0 關(guān)注
  • 119 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)