Defining a variable in a class and using it in functions

I'm trying to learn PHP classes, so I can start coding more OOP projects. To help me learn, I am creating a class that uses the Rapidshare API. Here is my class:

<?php

class RS
{
    public $baseUrl = 'http://api.rapidshare.com/cgi-bin/rsapi.cgi?sub=';

    function apiCall($params)
    {
        echo $baseUrl;
    }
}

?>

$params will contain a set of key pair values, for example:

$params = array(
    'sub'   =>  'listfiles_v1',
    'type'  =>  'prem',
    'login' =>  '746625',
    'password'  =>  'not_my_real_pass',
    'realfolder'    => '0',
    'fields'    => 'filename,downloads,size',
    );

Which will be added later $baseUrlto make the final request url, but I cannot get $ baseUrl to appear in my method apiCall(). I tried the following:

var $baseUrl = 'http://api.rapidshare.com/cgi-bin/rsapi.cgi?sub=';

$baseUrl = 'http://api.rapidshare.com/cgi-bin/rsapi.cgi?sub=';

private $baseUrl = 'http://api.rapidshare.com/cgi-bin/rsapi.cgi?sub=';

And even tried $this->baseUrl = $baseUrl;in my method apiCall(), I don’t know what the hell I was thinking there, although lol.

Any help is appreciated :)

+5
source share
3

Try

function apiCall($params)
{
    echo $this->baseUrl;
}

, ?

$rs = new RS;
$rs->apiCall($params);

PHP $this. self.

+12

:

class C
{
    public $v = 'Hello, world!';

    function printHello()
    {
        echo $this->v;   // "Hello, world!"
    }
}

$obj = new C();
$obj->printHello();
+2
function apiCall($params)
{
    echo $this->baseUrl;
}

int . $this->baseUrl = $baseUrl; var $baseUrl var $baseUrl, , , NULL

0

All Articles