How do I use a variable within an extended class public variable

Go To StackoverFlow.com

2

Have a class that I am using, I am overriding variables in the class to change them to what values I need, but I also not sure if or how to handle an issue. I need to add a key that is generated to each of this URLs before the class calls them. I cannot modify the class file itself.

use Theme/Ride

class ETicket extends Ride {

  public $key='US20120303'; // Not in original class
  public $accessURL1 = 'http://domain.com/keycheck.php?key='.$key;
  public $accessURL2 = 'http://domain.com/keycheck.php?key='.$key;

}

I understand that you cannot use a variable in the setting of the public class variables. Just not sure what would be the way to actually do something like this in the proper format.

My OOP skills are weak. I admit it. So if someone has a suggestion on where I could read up on it and get a clue, it would be appreciated as well. I guess I need OOP for Dummies. =/

---- UPDATE ---

The initial RIDE class has 2 URLs set.

public $accessURL1 = "http://domain.com/index.php";
public $accessURL2 = "http://domain.com/index2.php";

I was to override them so the RIDE class will use my new domains.

I can add the following and it works...

class ETicket extends RIDE {
    public $accessURL1 = 'http://mydomain.com/myindex.php';
    public $accessURL2 = 'http://mydomain.com/myindex2.php';
}

However, I also want to pass a variable from elsewhere ($key) as a parameter to the URL when I override them so when i call RIDE it has a URL with the value of KEY at the end. (?key=keyvalue)

2012-04-04 05:24
by Gerry Humphrey


1

Your close, if you do not want to allow calling code to change the $key, you can do something like:

class ETicket extends Ride {

    public function getKey()
    {
        return 'US20120303';
    }       

    public function generateUrl()
    {
        return 'http://domain.com/keycheck.php?key=' . $this->getKey();
    }
}

// Calling code example
$eTicket= new ETicket();

// $key is a member of ETicket class, so just call on generateUrl which will 
// build and return the url
var_dump($eTicket->generateUrl());

You can also permit calling code to change the key if needed, by adding a public setter/getter:

class ETicket extends Ride {

    protected $key;

    public function setKey($key)
    {
        $this->key = $key;
    }

    public function getKey()
    {
        return $this->key;
    }

    public function generateUrl()
    {
        return 'http://domain.com/keycheck.php?key=' . $this->getKey();
    }
}

// Calling code example
$eTicket= new ETicket();

$eTicket->setKey('US20120303');
var_dump($eTicket->generateUrl());

-- UPDATE --

There are a couple of options, you can either append the key to your url as part of the calling code, like this:

$eTicket= new ETicket();

$url = $ride->accessURL1 . '?key=US20120303';

Or, use a method (changed slightly to accept key directly) as I described earlier:

class ETicket extends Ride
{
    public function generateUrl($key)
    {
        return $this->accessURL1 . '?key=' . $key;
    } 
}

$eTicket= new ETicket();

$url = $eTicket->generateUrl('US20120303');

I guess the point is, you cannot do what you originally asked without which is to concatenate a variable to a member variable initialization.

2012-04-04 05:38
by Mike Purcell
Hi, did this answer help at all - Mike Purcell 2012-04-05 03:58
Sorry for the delay in a response.

I get what you are doing, but not sure how to implement it within the environment I am using. I am guessing that it is my description of the issue that I seem to have.

I will try to expand the initial question - Gerry Humphrey 2012-04-10 05:11

Ads