php 在 Codeigniter 中从类创建对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1513685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:53:22  来源:igfitidea点击:

Creating an object from a class in Codeigniter

phpcodeignitersnoopy

提问by shin

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

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

This is an example of using snoopyin codeigniter.

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

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?

因为 Snoopy.php 不创建对象。下面的例子是这样做的方法吗?如果是这样,您能否详细解释/指导我如何做的教程或解释?

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

Q2. Why do the author use

Q2。作者为什么用

$to_specialchars=true

Is it needed for this?

这是需要的吗?

Q3. Could you explain APPPATH and EXT.

Q3。你能解释一下 APPPATH 和 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?

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

Thanks in advance.

提前致谢。

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

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

I have application/library/Snoopy.php

我有应用程序/图书馆/史努比.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

我有一个控制器应用程序/控制器/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

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

回答by Matthew Rapati

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.

是因为如果您尝试使用 $this->load->library() 会得到一个致命错误,因为加载器类在库中不可用。你可以在控制器中调用它是因为你的控制器扩展了控制器类,它扩展了 ci_base 类,它扩展了 ci_loader 类,这是调用 $this->load 之类的功能的来源。您在此处展示的 Scraping 类没有。如果你深入挖掘,你会发现加载器基本上使用 include_once 来包含你尝试使用的任何类、助手等。

Q2.

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:

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

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.

如果您查看该函数的 return 语句,您会看到它们正在检查 $to_specialchars,如果为真,则输出首先通过 PHP 函数 htmlspecialchars() 运行。

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

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

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

and APPATH:

和 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 :)

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

回答by mataga

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

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



我试着再发一次。但我不能用超链接发帖。对不起..我会在我的网站上回答这个问题。(我是一个新手 stackoverflow.com :-()

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

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

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

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