doumao6212 2015-03-11 20:38
浏览 48

为什么我的PHP中的getter和setter看不到我私下声明的变量? (他们都在同一个班级)

I'm scraping data on cryptographers for a research project I'm doing for school. I have a really simple class that goes to a webpage, enters each of that page's href links, and writes them to a file.

I'm not actually getting a specific error when I run the code, but right now it just writes a blank file. My issue seems to be that my getters and setters have no knowledge of my private instance variables, and furthermore, my object ($obj) seems to have no knowledge of my getters and setters so I'm a bit confused.

I'm using JetBrains PHPStorm. Thanks to everyone for the help and support

Edit: I've updated the code below and it will run just fine. For anyone interested in using it - this code will scrape all of the links off of a web page and store the contents of each link inside of a file. I'm probably going to end up modifying this to strip out all html so that I only get raw text and then JSON-encode the output so that it can be easily parsed.

<?php
class Scraper
{

    /*
    =============================================
    SET UP THE BASE DIRECTORY FOR SCRAPING,
    AND OPEN FILES TO WRITE INFORMATION TO
    ==============================================
    */

    private $basedir; //BASE DIRECTORY PATH FOR SCRAPING
    private $outfile; //NAME OF FILE TO WRITE TO

    /*
    =============================================
    SETTER FOR BASE DIRECTORY
    ==============================================
    */

    public function setBaseDirectory($base)
    {
        $this->basedir = $base;
    }

    /*
    =============================================
    SETTER FOR OUTFILE
    ==============================================
    */

    public function setOutfile($file)
    {
        $this->outfile = $file;
    }

    /*
    =============================================
    GETTER FOR OUTFILE
    ==============================================
    */

    public function getOutfile()
    {
        return $this->outfile;
    }

    /*
    =============================================
    GETTER FOR BASE DIRECTORY
    ==============================================
    */

    public function getBaseDirectory()
    {
        return $this->basedir;
    }


    /*
    =============================================
    THIS FUNCTION TAKES THE HYPERLINKS OUT OF
    A WEB PAGE AND RETURNS THEM IN AN ARRAY.
    ITS SCOPE IS PRIVATE SINCE IT IS A HELPER
    METHOD FOR GETDIRCONTENTS
    =============================================
    */
    private function grabLinks($contents)
    {

        $last_dir = array();
        $URLs = array();

        preg_match_all("|href=[\"'](.*?)[\"']|", $contents, $match);

        foreach ($match as $key => $value)
            foreach ($value as $key2 => $TheUrl)
                $URLs[] = $TheUrl;

        for ($i =0; $i < (count($URLs)/2);$i++)
        {
            $item = str_replace('href=','',(string)$URLs[$i]);
            $item = str_replace('"','',$item);
            array_push($last_dir, $item);
        }

        return $last_dir;
    }


    /*
    =============================================
    THE GOAL OF THIS FUNCTION IS TO GET THE
    CONTENTS OF EACH FORUM POST AND WRITE THEM
    INTO A FILE. MAY EXPLORE CREATING AN
    ASSOCIATIVE ARRAY AND JSON_ENCODING THEM
    BASED ON NAME = POST NAME VALUE = FILE CONTENTS
    =============================================
    */
    public function getDirContents($dir)
    {

        $contents = file_get_contents($dir);
        $linksArray = $this->grabLinks($contents);
        for ($i = 0; $i < count($linksArray);$i++)
        {
            $contents = strip_tags(file_get_contents($dir.$linksArray[$i])); //GET CONTENTS OF FILE FROM LINK
            fwrite($this->getOutfile(), $contents);
            $debug = fopen("debugLog.txt", "w");
            fwrite($debug, "debug contents: 

".$this->getOutfile().$this->getBaseDirectory()." $contents 

");
        }
    }
}

/*
=============================================
CREATE NEW INSTANCE OF CLASS AND CALL FUNCTION
TO GET CONTENTS OF DIRECTORY ITEMS
==============================================
*/
$obj = new Scraper();
$obj->setBaseDirectory("http://satoshi.nakamotoinstitute.org/posts/");
$obj->setOutfile(fopen("Satoshi_Forum_Posts.txt", "w"));
$obj->getDirContents($obj->getBaseDirectory());
echo $obj->getBaseDirectory();
echo $obj->getOutfile();
  • 写回答

1条回答 默认 最新

  • dongzhijing8202 2015-03-11 21:47
    关注

    Ok, I've been able to locate the source of the problem and I apologize for wasting the time of those individuals who were kind enough to comment above. It turns out that my PHP code was just fine and ran after I made 1 change.

    I just started using JetBrains PHPStorm IDE, and loaded this class file into the editor from my desktop rather than the JetBrains' workspace. Once I incorporated the small syntactical changes mentioned by Bulk and Jonathan Kuhn I created a new project in JetBrains inside of the workspace I defined upon setting up the program and all of the warning messages went away (I still don't fully understand why they went away).

    I ran my code and produced the desired result. I'll post the updated code in the question with the updates suggested in the comments so that anyone who needs a script like this can use it. Thanks again for everyone willing to help out!

    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值