PHP Class Question

Am I allowed to use the same argument names in a function within a class? For example, with $time_out as shown below…

public function SetTimeOut($time_out)
{
    $this->SetOption(CURLOPT_TIMEOUT, $time_out);
}

public function SetConnectionTimeOut($time_out)
{
    $this->SetOption(CURLOPT_CONNECTTIMEOUT, $time_out);
}

yes

1 Like

Okay, I think I’ve got a basic understanding of how to create classes in OOP. Since my site relies heavily on Curl, I’ve written a curl class with the help of some tutorials. Before I go ahead and add to it I need to know if I am on the right lines or not…

<?php

class Curl
{
    
    public function __construct()
    {
        $this->curl = curl_init();
    }
    
    public function SetOption($option, $value)
    {
        $this->options[$option] = $value;
        return curl_setopt($this->curl, $option, $value);
    }
    
    public function SetCookieFile($cookie_file)
    {
        $this->setOption(CURLOPT_COOKIEFILE, $cookie_file);
    }
    
    public function SetCookieJar($cookie_jar)
    {
        $this->SetOption(CURLOPT_COOKIEJAR, $cookie_jar);
    }
    
    public function SetURL($url)
    {
        $this->SetOption(CURLOPT_URL, $url);
    }
    
    public function SetProxy($proxy, $port)
    {
        $this->SetOption(CURLOPT_PROXY, $proxy);
        $this->SetOption(CURLOPT_PROXYPORT, $port);
    }
    
    public function SetUserAgent($user_agent)
    {
        $this->SetOption(CURLOPT_USERAGENT, $user_agent);
    }
      
    public function SetTimeOut($time_out)
    {
        $this->SetOption(CURLOPT_TIMEOUT, $time_out);
    }
    
    public function SetConnectionTimeOut($time_out)
    {
        $this->SetOption(CURLOPT_CONNECTTIMEOUT, $time_out);
    }
    
    public function Get()
    {
        $this->SetOption(CURLOPT_CUSTOMREQUEST, 'GET');
        $this->setOption(CURLOPT_HTTPGET, true);
        return curl_exec($this->curl);
    }    
    
}

?>

I then call the class like so…

<?php

require_once 'curl.class.php';

$curl = new Curl();

$curl->SetURL('http://www.site.com/');
$curl->SetCookieJar('/home/site/public_html/cookie.txt');
$html = $curl->Get();

echo $html;

?>

One thing I am unsure about is the Curl Handler variables ($ch). In my old code is used within the curl_setopt functions, then later with the curl_exec() function. Do I not need to use any handler within this class, or is the handler now $this->curl ?

why not using one of the existing cURL libraries? they’re tried and tested …

Because using someone else’s code won’t help me learn.

1 Like

Yes.

I disagree. You will learn plenty.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.