Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我在Codeigniter中的第一个OOP aproach_Php_Oop - Fatal编程技术网

Php 我在Codeigniter中的第一个OOP aproach

Php 我在Codeigniter中的第一个OOP aproach,php,oop,Php,Oop,我正在尝试制作一个爬虫程序,从不提供任何API访问的站点获取模板,以便以后作为附属站点显示。 我刚开始学习CI,阅读了几次文档,下面您可以找到我的第一个OOP方法。 我的问题是,我是否走上了OOP的正确道路,或者我的代码是否(我确信)有任何改进。我在网上读了很多OOP教程,人们似乎对OOP编码有不同的看法 先谢谢你 <?php class Crawler extends CI_Model { function __construct(){ parent::__construct(

我正在尝试制作一个爬虫程序,从不提供任何API访问的站点获取模板,以便以后作为附属站点显示。 我刚开始学习CI,阅读了几次文档,下面您可以找到我的第一个OOP方法。 我的问题是,我是否走上了OOP的正确道路,或者我的代码是否(我确信)有任何改进。我在网上读了很多OOP教程,人们似乎对OOP编码有不同的看法

先谢谢你

<?php
class Crawler extends CI_Model {

function __construct(){
    parent::__construct();
}

function get_with_curl($url) {
    if(!ini_get('allow_url_fopen')) {
        return $this->html = file_get_html($url);
    } else {
        $curl = curl_init(); 
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
        $str = curl_exec($curl);  
        curl_close($curl);  

        return $this->html = str_get_html($str);
    }
}

function array_arrange($links){
    $links = array_merge(array_unique($links));
    foreach (range(1, count($links), 2) as $k) {
        unset($links[$k]);
    }
    return array_merge($links);
}

function diff($source,$links){
    $this->db->like('source', $source);
    $this->db->from('themes');
    $total = $this->db->count_all_results();

    if($total >= count($links)){
        return false;
    } else {
        $diff = count($links)-$total;
        $data = array_slice($links,-$diff,$diff,true);
        return $data;
    }
}

function get_links($url,$find){
    $this->html = $this->get_with_curl($url);
    foreach($this->html->find($find) as $v){
        $data[] = $v->href;
    }
    $this->html->clear();
    unset($this->html);

    return $data;
}

function themefyme(){
    $links = $this->get_links('http://themify.me/themes','ul[class=theme-list] li a');
    $links = $this->array_arrange($links);
    $links = $this->diff('themefyme',$links);
    if($links){
        $i = 0;
        foreach($links as $link){
            $this->html = $this->get_with_curl($link);
            $data[$i]['source']         = 'themefyme';
            $data[$i]['name']           = strtok($this->html->find('h1', 0)->plaintext,' ');
            $data[$i]['link']           = $link;
            $data[$i]['demo']           = 'http://themify.me/demo/#theme='.strtolower($data[$i]['name']);
            $data[$i]['price']          = filter_var($this->html->find('h1 sup', 0)->plaintext, FILTER_SANITIZE_NUMBER_INT);
            $data[$i]['description']    = $this->html->find('big', 0)->plaintext;
            $data[$i]['features']       = $this->html->find('ul', 0)->plaintext;
            $data[$i]['img_large']      = $this->html->find('.theme-large-screen img', 0)->src;
            $data[$i]['img_thumb']      = 'http://themify.me/wp-content/themes/themify/thumb.php?src='.$data[$i]['img_large'].'&q=90&w=220';
            $i++;

            $this->html->clear();
            unset($this->html);
        }
        $this->db->insert_batch('themes', $data); 
        return $data;
    }
    return false;
}

function themefuse(){
    $links = $this->get_links('http://www.themefuse.com/wp-themes-shop/','.theme-img a');
    $links = $this->array_arrange($links);
    $links = $this->diff('themefuse',$links);
    if($links){
        $i = 0;
        foreach($links as $link){
            $this->html = $this->get_with_curl($link);
            $data[$i]['source']         = 'themefuse';
            $data[$i]['name']           = $this->html->find('.theme-price', 0)->plaintext;
            $data[$i]['link']           = $link;
            $data[$i]['demo']           = 'http://themefuse.com/demo/wp/'.strtolower($data[$i]['name']).'/';
            $data[$i]['description']    = $this->html->find('.short-descr', 0)->plaintext;
            $data[$i]['highlights']     = $this->html->find('.highlights', 0)->outertext;
            $data[$i]['features']       = $this->html->find('.col-features', 0)->outertext;
            $data[$i]['theme_info']     = $this->html->find('.col-themeinfo', 0)->outertext;
                                          preg_match("/src=(.*?)&amp;/",$this->html->find('.slideshow img', 0)->src, $img);
            $data[$i]['img_large']      = $img[1];
            $data[$i]['img_thumb']      = 'http://themefuse.com/wp-content/themes/themefuse/thumb.php?src='.$img[1].'&h=225&w=431&zc=1&q=100';
            $i++;

            $this->html->clear();
            unset($this->html);
        }
        $this->db->insert_batch('themes', $data); 
        return $data;
    }
    return false;
}
}

正如PeeHaa所说,您的示例并不是真正的OOP。OOP意味着面向对象编程,这基本上意味着类(对象)应该像表示物理对象一样表示实体。因此,您的类将是一组与对象相关的函数(方法)

例如,一个
机器人
对象可能具有向前移动、向后移动、说话等功能


您可能有另一种机器人类型,它可以做
机器人
对象可以做的所有事情,但方式略有不同。例如,您可能有一个
MoonRobot
对象,它扩展了
Robot
对象(它将继承所有
Robot
的函数),但它的
MoonRobot
函数可能不同,因此可以在
MoonRobot
类中进行更改。

这不是OOP。这些都是封装在一个类中的函数。而且,如果没有codeigniter,您可能会更好,因为它不专业,很可能会导致糟糕的代码质量。Zend或Symfony2是一个更好的主意。要回答您的问题,您需要查找大量的内容,如关注点分离、依赖项注入以及使用接口、设计模式、单一责任原则等等。。。你可能想知道这会有什么回报?好吧,高质量的OO代码通过可读性、可重用性、可维护性和可扩展性得到了很好的回报。相信我,这是值得的。你的问题有确切的解决办法吗?这似乎有点含糊不清……更确切地说,
MoonRobot实现了Robot
:)毕竟,您需要替换方法实现。但你的解释是对的。@Powerslave说得对!你可以使用一个机器人接口,但是如果你有一个机器人MK1,你用一个赛格威扩展你的机器人,他的向前移动功能将是向前倾斜而不是移动他的腿:)只是开玩笑来扩展(或“实现”)@MajorCaiger的实例,你可以构建一个“Crawler”类,其中包含“MeCrawler可以执行的任何操作”,包括一个getTheme()函数。然后,您可以构建第二个名为FymeCrawler的类,该类“实现(认为“继承”)爬虫程序”。在FymeCrawler中,您将使用当前ThemeFyme函数中的代码重写getTheme()函数。然后,您可以创建第三个类,该类还“实现”了名为ThemeFuse的爬虫程序,其getTheme()函数还使用特定于保险丝的代码覆盖Crawler::getTheme()Themes@MajorCaiger哈哈哈!我喜欢segways btwIt,它不必是物理对象,抽象概念也可以,事实上可能更常见。