简体   繁体   English

如何在扩展的 class 公共变量中使用变量

[英]How do I use a variable within an extended class public variable

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.有一个我正在使用的 class,我正在覆盖 class 中的变量以将它们更改为我需要的值,但我也不确定是否或如何处理问题。 I need to add a key that is generated to each of this URLs before the class calls them.我需要在 class 调用它们之前为每个 URL 添加生成的密钥。 I cannot modify the class file itself.我无法修改 class 文件本身。

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.我知道您不能在公共 class 变量的设置中使用变量。 Just not sure what would be the way to actually do something like this in the proper format.只是不确定以正确的格式实际执行此类操作的方法是什么。

My OOP skills are weak.我的 OOP 技能很弱。 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.我想我需要 OOP 作为 Dummies。 =/ =/

---- UPDATE --- - - 更新 - -

The initial RIDE class has 2 URLs set.最初的 RIDE class 设置了 2 个 URL。

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.我要覆盖它们,以便 RIDE class 将使用我的新域。

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)作为参数传递给 URL,因此当我调用 RIDE 时,它有一个 URL,其末尾的值为 KEY。 (?key=keyvalue) (?键=键值)

Your close, if you do not want to allow calling code to change the $key, you can do something like:您关闭,如果您不想让调用代码更改 $key,您可以执行以下操作:

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:如果需要,您还可以通过添加公共 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:有几个选项,您可以将 append 作为调用代码的一部分作为 url 的密钥,如下所示:

$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.我想关键是,如果没有将变量连接到成员变量初始化,你就不能做你最初要求的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM