Home > Backend Development > PHP Tutorial > 一个面向对象方面的很基础的小白有关问题,求指点

一个面向对象方面的很基础的小白有关问题,求指点

WBOY
Release: 2016-06-13 12:32:38
Original
928 people have browsed it

一个面向对象方面的很基础的小白问题,求指点
在一个类内(其实是thinkPHP的控制器内类),有很多个方法(不是全部方法)会用到一些语句,我希望能将其整合到一个地方(比如一个方法里边?)到处调用一下,求教该怎么写

贴出现在的代码样例(是有错误的)

<br />
class AbcAction extends BbcAction{<br />
    public function init(){<br />
        $model = M("table1");<br />
        $a = $model-> where("id = 1") -> getField("id");   //$a = 1<br />
        $this -> assign("a",$a);<br />
    }<br />
<br />
    public function test(){<br />
	$this->init()<br />
	echo $a;  <br />
	$model -> find(1);<br />
	//希望在这里能用到上边的$a和$model(实际上这样写是报错的,因为这是init内部的变量?<br />
	//init向前台传递的这个值,好像是可以的<br />
    } <br />
}<br />
Copy after login


问题很小白,求不吝赐教(眼下就要用到,来不及去系统的学习面向对象了..)
面向对象 方法内的变量

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