Creating an object from a class in Codeigniter

Below are the codes from http://d.hatena.ne.jp/dix3/20081002/1222899116 , and the codes work well.

This is an example of using snoopy in codeigniter.

Q1. Am I saying correctly that I cannot use,

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

since Snoopy.php does not create an object. And the example below is a way to do this? If so, can you explain / send me the textbook or explain how to do this in detail?

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

Q2. Why is the author using

 $to_specialchars=true 

Is this necessary 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 should be an extension, but can it be used anywhere?

Thanks in advance.

I have snoopy in /library/Snoopy.php application

I have an 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> 
+2
source share
2 answers

Q1. You can use:

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

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

 $snooper = new Snoopy(); 

The reason they are used:

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

This is because you get a fatal error if you try to use $ this-> load-> library (), because the loader class is not available in the library. You can call it in the controller because your controllers extend the controller class, which extends the ci_base class, which extends the ci_loader class, in which there is functionality for making calls such as $ this-> load. The Scraping class that you showed here does not work. If you dig, you will see that the loader mainly uses include_once to include any class, helper, etc. that you are trying to use.

Q2.

 $to_specialchars = true 

used in a pair of function declarations as parameters. Setting it to '= true' just sets the default value, so you 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 this function, you will see that they are checked for $ to_specialchars, and if it is true, then the output is first run through the PHP function htmlspecialchars ().

Q3. If you look at the root of your codeigniter project, in index.php you will see that EXT is 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 that are set at boot, so you can use them in your application, and if you ever changed them, then this would not be examples, for example, when you see that they are used in code, which you provided.

Next time you will have one question to ask about the stack :)

+15
source

. This sample scrambling code was written using the library: "Snoopy - PHP Network Client (snoopy.sourceforge.net)"


I tried to publish it again. but I could not write hyperlinks. Sorry .. I will answer this on my website (I am new to stackoverflow.com :-()

I think that in a few days I will try to fulfill these answers.

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

0
source

Source: https://habr.com/ru/post/1306633/


All Articles