drz49609 2011-12-24 15:30
浏览 53
已采纳

PHP类返回值和设置变量

i wanted move to OOP, and trying to understand it. So far i made my first class. Here is the Code. (The problem is with the function sCURL() and returning values and access it right.

    class CURL {

    public $url; 
    private $header = false; // DISPLAY HEADERS (FALSE OR TRUE)
    private $follow = true; // FOLLOW REDIRCETS (FALSE OR TRUE)
    private $useragent = "Mozilla/4.0 (compatible; MSIE 6.0;Windows NT 5.1)"; // SET USER AGENT e.g. "Mozilla/4.0 (compatible; MSIE 6.0;Windows NT 5.1)"
    private $referer = "http://www.google.com"; // SET REFERER e.g. http://www.google.com
    private $ssl = false; // If set false (it accpets any ssl) should false
    private $ctimeout = 5; // Timeout for connect in SECs when curl does next url
    private $timeout = 60; // Timeout of retriving page in SECs when curl does next url


    public function setHeader($header) {
        $this->header = $header;
    }
    public function setFollow($follow) {
        $this->follow = $follow;
    }
    public function setUseragent($useragent) {
        $this->useragent = $useragent;
    }
    public function setReferer($referer) {
        $this->referer = $referer;
    }
    public function setSsl($ssl) {
        $this->ssl = $ssl;
    }
    public function setCtimeout($ctimeout) {
        $this->ctimeout = $ctimeout;
    }
    public function setTimeout($timeout) {
        $this->timeout = $timeout;
    }
    public function __construct($url) {
        $this->url = $url;
    }

    public function sCURL() {

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $this->url);
        curl_setopt($ch, CURLOPT_HEADER, $this->header);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $this->follow);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_USERAGENT, $this->useragent);
        curl_setopt($ch, CURLOPT_REFERER, $this->referer);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->ssl);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->ctimeout);
        curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);

        $data = curl_exec($ch);
        curl_close($ch);

        return $data;
    }
}

and this are the results:

object(CURL)#1 (9) {
  ["url"]=>
  string(23) "http://www.facebook.com"
  ["header:private"]=>
  bool(false)
  ["follow:private"]=>
  bool(true)
  ["useragent:private"]=>
  string(49) "Mozilla/4.0 (compatible; MSIE 6.0;Windows NT 5.1)"
  ["referer:private"]=>
  string(21) "http://www.google.com"
  ["ssl:private"]=>
  bool(false)
  ["ctimeout:private"]=>
  int(5)
  ["timeout:private"]=>
  int(60)
  ["data"]=>
  NULL
}

as you can see "data" = NULL.

than i replaced this peace of code

    $data = curl_exec($ch);
    curl_close($ch);

    return $data;

with this:

    $this->data = curl_exec($ch);
    curl_close($ch);

    return $this->data;

and now this are the results (Working):

object(CURL)#1 (9) {
  ["url"]=>
  string(23) "http://www.facebook.com"
  ["header:private"]=>
  bool(false)
  ["follow:private"]=>
  bool(true)
  ["useragent:private"]=>
  string(49) "Mozilla/4.0 (compatible; MSIE 6.0;Windows NT 5.1)"
  ["referer:private"]=>
  string(21) "http://www.google.com"
  ["ssl:private"]=>
  bool(false)
  ["ctimeout:private"]=>
  int(5)
  ["timeout:private"]=>
  int(60)
  ["data"]=>
  string(33320) "<!DOCTYPE html>.........STRIPPED OUTBUT THATS WHAT I WANTED........"

Ok so this is how i call the class

$data1 = new Curl("http://www.facebook.com");
$data1->sCURL();

var_dump($data1);

this gives me the above results. Here is my problem i want access only the "DATA" thing.

$data1 = new Curl("http://www.facebook.com");
$data1->sCURL();

var_dump($data1['data']);

if try to access key 'data' i get this error

Fatal error: Cannot use object of type CURL as array in * on line 10

So, how i can access the data, direct (the array $data1['data'], and also would you change something from my class to make it better? And for my understanding, why did that return $data; in the first example class not worked. I googled and googled but dont found an answer. Sorry, i just started using OOP before i made a huge list of functions.

UPDATE when i set

curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 

To false

curl_setopt($ch, CURLOPT_RETURNTRANSFER, false); 

i now can access the data

$data1['data'] , BUT the problem somehow still remains, $data1['data'] gets displayed above at the end i get this:

Fatal error: Cannot use object of type CURL as array in * on line 

im just curious, why i cant access if its set true, if somebody can explain that i would be happy.

Thank you for your time.

and

MERRY XMAS TO ALL :-)

  • 写回答

3条回答 默认 最新

  • douwen4125 2011-12-24 15:35
    关注

    In your example:

    $data1 = new Curl("http://www.facebook.com");
    $data1->sCURL();
    
    var_dump($data1['data']);
    

    $data1 is an object, you cannot use an object as an array(apart if you define a specific interface).

    What you can do would be something like

    $data1 = new Curl("http://www.facebook.com");
    $data1->sCURL();
    
    var_dump($data1->data);
    

    Also it's not generally a good practice to leave public the properties, a better approach is to make the property private and create some methods to access it, it's call getter and setter example:

    class CURL {
        private $data;
    
        ...
        public function getData() {
           return $this->data;
        }
    
        public function setDate($data) {
           $this->data = $data;
        }
    }
    

    Using getter and setter you have a better control over the data, to be noted that in your example the setter won't be needed.

    Last point, it's generally a good practice to define al the property, I think you forgot the declaration of the data property in your class.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 请分析一下这个电路设计的优点🙏
  • ¥15 求视频摘要youtube和ovp数据集
  • ¥15 怎么改成输入一个要删除的数后现实剩余的数再输入一个删除的数再现实剩余的数用yes表示继续no结束程序
  • ¥15 在启动roslaunch时出现如下问题
  • ¥15 汇编语言实现加减法计算器的功能
  • ¥20 关于多单片机模块化的一些问题
  • ¥30 seata使用出现报错,其他服务找不到seata
  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取