在 Codeigniter 中从类创建对象

发布于 2024-08-07 04:22:53 字数 4191 浏览 5 评论 0原文

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

这是在 codeigniter 中使用 snoopy 的示例。

Q1.我是否正确地说我不能使用,

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

因为 Snoopy.php 不创建对象。下面的例子是如何做到的? 如果是这样,您能给我解释/指导我一个教程或解释如何详细执行此操作吗?

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

Q2。为什么作者要使用

$to_specialchars=true

Is it need for this?

Q3。你能解释一下 APPPATH 和 EXT 吗?

APPPATH.'libraries/Snoopy'.EXT

我在 php.net 上查了一下,但没找到。 EXT 必须是扩展名,但是我可以在任何地方使用吗?

提前致谢。

我在 application/library/Snoopy.php 有一个史努比

我有 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 */
 ?>

我有一个控制器 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 );
} 


}

我有一个视图 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>

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

This is an example of using snoopy in codeigniter.

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

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

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. Why do the author use

$to_specialchars=true

Is it needed for this?

Q3. Could you explain APPPATH and EXT.

APPPATH.'libraries/Snoopy'.EXT

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

Thanks in advance.

I have a snoopy in application/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 */
 ?>

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 );
} 


}

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

耀眼的星火 2024-08-14 04:22:53

Q1.您可以使用:

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

在您的控制器中。并像这样创建一个新实例:

$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 来包含你尝试使用的任何类、帮助器等。

Q2。

$to_specialchars = true

在几个函数声明中用作参数。设置它 '=true' 只是设置默认值,所以你可以这样做:

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

这与此相同:

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

如果你查看该函数的 return 语句,你会看到它们正在检查 $to_specialchars ,并且如果是的,那么首先通过 PHP 函数 htmlspecialchars() 运行输出。

Q3。如果您查看 codeigniter 项目的根目录,在 index.php 中,您将看到 EXT 定义为:

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.'/');
}

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

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

Q1. You can use:

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

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

$snooper = new Snoopy();

The reason they are using:

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

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

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');

Which is identical to this:

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

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. 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));

and 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.

Please next time have one question per stackoverflow question :)

柠檬 2024-08-14 04:22:53

。此示例抓取代码是基于使用该库编写的:
“Snoopy - PHP 网络客户端 (snoopy.sourceforge.net)”


I tried to post it again. but I couldn't post with hyperlinks. sorry..
I'll answer to that in my site.(I'm a newbie stackoverflow.com :-( )

我想几天后我会尝试重新发布这些答案。

( http://d.hatena.ne.jp/dix3/20091004 )

. This sample Scraping code was written based on using the library:
"Snoopy - the PHP net client ( snoopy.sourceforge.net )"


I tried to post it again. but I couldn't post with hyperlinks. sorry..
I'll answer to that in my site.(I'm a newbie stackoverflow.com :-( )

I think that I'll try to repost these answers after a few days .

( http://d.hatena.ne.jp/dix3/20091004 )

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文