Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6965069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:00:22+00:00 2026-05-27T16:00:22+00:00

i wanted move to OOP, and trying to understand it. So far i made

  • 0

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 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:00:23+00:00Added an answer on May 27, 2026 at 4:00 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have function X in file A, and I wanted to move that
Im having issues making the code below to work. Basically, I wanted to move
I've created small database under SQL Server 2008 . Now i wanted to move
Wanted to know if someone had a suggestion on code or maybe there's a
I wanted to move my image using keyboard arrow keys. when I pressed the
I wanted to move our existing repository (from location A) to a new linux
This morning I wanted to move my development website online (in a protected folder),
I ran the following command as I wanted to move my production branch back
I wanted to move the buttons added to my dialog box to either top
So basically I just wanted to create an enemy class with pygames.sprite.Sprite as the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.