thinkphp面向对象的问题

WBOY
Release: 2016-07-06 13:51:27
Original
1101 people have browsed it

TP框架分页类里面 一开始的时候我看那里定义了一个私有属性private $url     = '';
我看下面有两个地方用到了这个私有属性 
这个私有方法在这个show方法的上面  运行到这个私有方法时 $url这个属性是空的啊 不会报错吗?

<code>private function url($page){
        return str_replace(urlencode('[PAGE]'), $page, $this->url);
    }

public function show() {
        if(0 == $this->totalRows) return '';
        $this->parameter[$this->p] = '[PAGE]';
        $this->url = U(ACTION_NAME, $this->parameter);
……</code>
Copy after login
Copy after login

回复内容:

TP框架分页类里面 一开始的时候我看那里定义了一个私有属性private $url     = '';
我看下面有两个地方用到了这个私有属性 
这个私有方法在这个show方法的上面  运行到这个私有方法时 $url这个属性是空的啊 不会报错吗?

<code>private function url($page){
        return str_replace(urlencode('[PAGE]'), $page, $this->url);
    }

public function show() {
        if(0 == $this->totalRows) return '';
        $this->parameter[$this->p] = '[PAGE]';
        $this->url = U(ACTION_NAME, $this->parameter);
……</code>
Copy after login
Copy after login

你确定你了解编程吗?
所有的函数存在定义和调用两个过程,你看到的是两个定义的过程。
定义过程中存在的对变量的使用并不是真的就使用了变量,使用变量是在调用的过程中。
所以定义的顺序并不影响变量的值,又何来上面和下面之分。

str_replace 函数的描述上有句:该函数是二进制安全的。
二进制安全,在我理解上来说对输入任何字节都能正确处理, 即使包含零值字节.题主也可以搜下二进制安全这个概念

你的url到底是私有属性还是私有方法?

$this->url这个私有属性在你的show方法里面定义了,不会是空的

这回答都是啥啊。。。晕,是程序员吗?

首先他是类,你调用的也不是静态函数,所以url在创建类实例的时候就初始化了,他就是'',空的字符串,不是没有定义,是定义了但是是空的。所以不会有报错。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!