2 Star 0 Fork 0

狂奔的蜗牛. / soter-docs

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
library.html 5.56 KB
一键复制 编辑 原始数据 按行查看 历史
arraykeys 提交于 2017-07-19 11:41 . No commit message
<!DOCTYPE html>
<html>
<head>
<title>第三方类库</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="js/inc.js"></script>
</head>
<body>
<fieldset>
<legend>第三方类库</legend>
<ol>
<li>
<h2 class="title_h2">第三方类库规范</h2>
1.第三方类库文件都放在<code>application/library/classes</code>文件夹里面
<br>2.<b class="text_strong">文件名和类名一样,大小写敏感。</b>
<br>示例如下:
<br>1.新建类库文件application/library/classes/MyClass.php
<br>2.输入以下代码:
<pre class="brush:php">
&lt;?php
class MyClass {
public function testLibrary() {
echo 'hello world';
}
}
</pre>
</li>
<li>
<h2 class="title_h2">使用第三方类库</h2>
可以在任意地方使用下面的代码加载一个第三方类库:
<br>示例如下:
<pre class="brush:php">
Sr::library('MyClass');
//我们也可以直接用new如下
new MyClass();
</pre>
我们可以看到上面有一个参数,这个是第三方类库的<b>文件名</b>
</li>
<li>
<h2 class="title_h2">示例一</h2>
比如有文件:application/library/classes/MyClass.php
<br>那么MyClass.php文件里面的类名就应该是:MyClass。
<br>那么要加载第三方类库如下:
<pre class="brush:php">
Sr::library('MyClass');
</pre>
</li>
<li>
<h2 class="title_h2">创建自己的第三方类库</h2>
下面我们自定义一个加载第三方类库
<br>1.新建第三方类库文件application/library/classes/MyClass.php
<br>2.输入以下代码:
<pre class="brush:php">
&lt;?php
class MyClass {
public function helloWorld() {
echo 'hello world';
}
}
</pre>
3.新建控制器文件application/classes/Controller/Welcome.php
<br>4.输入以下代码:
<pre class="brush:php">
&lt;?php
class Controller_Welcome extends Soter_Controller {
public function do_library() {
$library = Sr::library('MyClass');
echo $library->helloWorld();
}
}
</pre>
5.浏览器访问:http://127.0.0.1/index.php/Welcome/library.do
<br>//将输出:hello world
<br>为什么会输出“<b>hello world</b>”?
<br>我们可以从上面例子看Controller_Welcome控制器类,
<br>在do_library方法里面做了加载第三方类库中的<code>MyClass</code>类赋值给<b>$library</b>
<br>然后通过<b>$library</b>输出<code>MyClass</code>类中的helloWorld方法,
<br>所以浏览器才会显示“<b>hello world</b>”。
</li>
</ol>
</fieldset>
<script src="js/inc.foot.js"></script>
</body>
</html>
1
https://gitee.com/snail/soter-docs.git
git@gitee.com:snail/soter-docs.git
snail
soter-docs
soter-docs
master

搜索帮助