Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining a variable in a class and using it in functions

I am trying to learn PHP classes so I can begin coding more OOP projects. To help me learn I am building a class that uses the Rapidshare API. Here's 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, like this:

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

Which will later be appended to $baseUrl to make the final request URL, but I can't get $baseUrl to appear in my apiCall() method. I have 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 apiCall() methid, I don't know what the hell I was thinking there though lol.

Any help is appreciated thanks :)

like image 745
Josh Avatar asked Mar 25 '10 16:03

Josh


People also ask

How do you use class variables in a function?

The variables that are defined inside the class but outside the method can be accessed within the class(all methods included) using the instance of a class. For Example – self. var_name. If you want to use that variable even outside the class, you must declared that variable as a global.

Can you define a variable in a class?

A class variable is an important part of object-oriented programming (OOP) that defines a specific attribute or property for a class and may be referred to as a member variable or static member variable.

Can you define variables in a function?

Function scopeVariables defined inside a function cannot be accessed from anywhere outside the function, because the variable is defined only in the scope of the function. However, a function can access all variables and functions defined inside the scope in which it is defined.

What is the function of a class variable?

In class-based, object-oriented programming, a class variable is a variable defined in a class of which a single copy exists, regardless of how many instances of the class exist. A class variable is not an instance variable. It is a special type of class attribute (or class property, field, or data member).


2 Answers

Try

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

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

I trust you are calling this code like so?

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

Class attributes need to be prefixed with $this in PHP. The only exceptions are static methods and class constants when you use self.

like image 111
Greg K Avatar answered Sep 18 '22 01:09

Greg K


Try this:

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

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

$obj = new C();
$obj->printHello();
like image 36
John Feminella Avatar answered Sep 19 '22 01:09

John Feminella