从Codeigniter中的类创建对象 [英] Creating an object from a class in Codeigniter

查看:107
本文介绍了从Codeigniter中的类创建对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码来自 http://d.hatena.ne.jp/ dix3 / 20081002/1222899116 和代码运行良好。

The following codes are from http://d.hatena.ne.jp/dix3/20081002/1222899116 and codes are working well.

这是使用 snoopy in codeigniter。

This is an example of using snoopy in codeigniter.

Q1。我正确地说我不能使用,

Q1. Am I correct to say that I can't use,

$this -> load -> library('snoopy')

,因为Snoopy.php不创建对象。下面的例子是怎么做的?
如果是,您能解释/指导我一个详细的教程或解释如何做?

since Snoopy.php does not create an object. And the example below is the way to do it? If so, can you explain/direct me an tutorial or explanation of how to do it in details?

if ( ! class_exists('Snoopy'))
    {
        require_once(APPPATH.'libraries/Snoopy'.EXT);
    }

Q2。为什么作者使用

Q2. Why do the author use

$to_specialchars=true

这是需要吗?

Q3。你能解释APPPATH和EXT。

Q3. Could you explain APPPATH and EXT.

APPPATH.'libraries/Snoopy'.EXT

我在php.net中检查过,但是找不到它。 EXT必须是扩展,但是我可以在任何地方使用?

I checked it in php.net but I could not find it. EXT must be extension, but can I use anywhere?

提前感谢。

我有一个snoopy application / library / Snoopy.php

I have a snoopy in application/library/Snoopy.php

我有应用程式/ library / Snoopy.php

I have application/library/Snoopy.php

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Scraping{
    var $c; 
    function Scraping(){
        if ( ! class_exists('Snoopy'))
        {
            require_once(APPPATH.'libraries/Snoopy'.EXT);
        }
        $this -> c = new Snoopy();
    }

    function getWebHtml($url="",$to_specialchars=true){
        $this ->c -> fetch( $url );
        $str = mb_convert_encoding( (string) $this -> c -> results,"UTF-8","auto");
        return ($to_specialchars) ? htmlspecialchars($str , ENT_QUOTES , "UTF-8" ) : $str ;
    }

   function getWebText($url="",$to_specialchars=true){
        $this -> c -> fetchtext( $url );
        $str = mb_convert_encoding( (string) $this -> c -> results,"UTF-8","auto");
        return ($to_specialchars) ? htmlspecialchars($str , ENT_QUOTES , "UTF-8" ) : $str ;
    }

    function getWebLinks($url=""){
        $this -> c -> fetchlinks( $url );
        return (array) $this-> c -> results ;
    }

    function getWebLinksText($url="",$delimiter="<br>"){
        $arr = $this-> getWebLinks($url) ;
        $ret ="";
        foreach($arr as $k => $v){
            $ret .= $v . $delimiter ;
        }
        return $ret;
    }

} //endofclass

/* End of file Scraping.php */
/* Location: ./application/libraries/Scraping.php */
 ?>

我有一个控制器应用程序/ controller / mytasklist.php

I have a controller application/controller/mytasklist.php

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Mytasklist extends Controller {

function Mytasklist()
{
  parent :: Controller(); 

  $this -> load -> helper( 'url' ); 

} 

    function index()
    {
      $data = "";

      $this -> _SetTpl( $data );
    } 
 function _SetTpl( $data )
{ 


  $this -> load -> library("scraping");
  $data["scraping"]["text"] = $this-> scraping -> getWebText("http://www.example.com/");
  $data["scraping"]["html"] = $this-> scraping -> getWebHtml("http://www.example.com/");
  $data["scraping"]["link"] = $this-> scraping -> getWebLinksText("http://www.example.com/","\n");

  $tpl["page_title"] = "Welcome";

  $tpl["main_content"] = $this -> load -> view( 'tasklist_view', $data , true ); 

  $this -> load -> view( 'base_view', $tpl );
} 


}

a view,application / view / base_view.php

And I have a view, application/view/base_view.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja">
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <meta name="keywords" content="keyword here" />
    <meta name="description" content="description here" />
    <title><?php if(isset($page_title)){echo $page_title ;}?></title>
    <?php if(isset($xajax_js)){echo $xajax_js ;}?>
    <link href="http://127.0.0.1/ci_day4/css/mystyle.css" rel="stylesheet" type="text/css"/>
</head>
<body>

<div id="container">


    <div id="rightblock">

        <div id="content">

            <?=$main_content?>

        </div>

    </div>

</div>

</body>
</html>


推荐答案

Q1。您可以使用:

$this->load->library('snoopy');

在您的控制器中。并创建如下所示的新实例:

In your controllers. And create a new instance like so:

$snooper = new Snoopy();

他们使用的原因:

if (!class_exists('Snoopy')) {
    require_once(APPPATH.'libraries/Snoopy'.EXT);
}

是因为如果你尝试使用$ this- > load-> library(),因为加载器类在库中不可用。你可以在控制器中调用它,因为你的控制器扩展了控制器类,它扩展了ci_base类,它扩展了ci_loader类,这是调用$ this-> load的功能。你在这里显示的Scraping类不。如果你挖掘,你会看到加载程序基本上是使用include_once来包括你想要使用的任何类,助手等。

Is because you will get a fatal error if you try and use $this->load->library(), since the loader class is not available in the library. You can call it in a controller is because your controllers extend the controller class, which extends the ci_base class, which extends the ci_loader class which is where the functionality to make calls like $this->load comes from. The Scraping class that you've shown here does not. If you dig down you'll see that the loader is basically using include_once to include whatever class, helper etc. you're trying to use.

Q2。

$to_specialchars = true


$ b b

用于将函数声明作为参数。设置为'= true'只是设置默认值,所以你可以这样做:

is being used in a couple the function declarations as parameters. Setting it '=true' is just setting a default, so you could can do this:

echo $scrappy->getWebHtml('http://example.com');

与此相同:

echo $scrappy->getWebHtml('http://example.com', true);

如果你看一下函数的return语句,你会看到它们是$ to_specialchars检查,如果它是真的,则输出通过PHP函数htmlspecialchars()运行。

If you look at the return statement of that function, you'll see they are $to_specialchars is being checked, and if it's true, then the output is run through the PHP function htmlspecialchars() first.

Q3。如果你查看你的codeigniter项目的根,在index.php你会看到EXT定义为:

Q3. If you look at the root of your codeigniter project, in index.php you'll see EXT defined as:

define('EXT', '.'.pathinfo(__FILE__, PATHINFO_EXTENSION));

和APPATH:

if (is_dir($application_folder))
{
define('APPPATH', $application_folder.'/');
}
else
{
    if ($application_folder == '')
    {
        $application_folder = 'application';
    }
    define('APPPATH', BASEPATH.$application_folder.'/');
}



这些是在引导时设置的两个常量,因此您可以使用它们你的应用程序,如果你要更改它们,那么它不会像你看到它在您提供的代码中使用的实例。

So these are two constants being set at bootstrapping, so you can use them in your application, and if you were to ever change them, then it wouldn't instances like where you see it being used in the code you provided.

请下次每个stackoverflow问题一个问题:)

Please next time have one question per stackoverflow question :)

这篇关于从Codeigniter中的类创建对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆