PHP 限制调用公共方法 [英] PHP restrict calling of public methods

查看:44
本文介绍了PHP 限制调用公共方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个库,该库有相当多的类,这些类都由一个中心类组合在一起.为了设置/配置目的,这个中心类必须调用其他类上的某些方法.这些方法必须是公共的,以便中心类可以调用它们,但我不希望用户调用这些方法(因为它可能会导致不受欢迎的行为).

I'm working on a library that has quite a few classes that are all pulled together by a central class. This central class has to call certain methods on the other classes for set-up/configuration purposes. These methods have to be public so that the central class can call them, but I do not want users to call these methods (as it may cause undesired behavior).

我的一个朋友提出的一个建议是使用带有参数的构造函数可以在构造时完成设置.就本库而言,这并不容易.有问题的类旨在扩展,如果用户想要拥有自己的构造函数,我不想对构造函数参数强加任何奇怪的要求.更复杂的是,用户无法获得一些用于配置的信息.我必须将其提供给用户并相信他们会在构建期间将其路由回正确的类.

One suggestion a friend of mine made was to use a constructor with parameters can have the set-up done at construction time. For the purposes of this library, that is not easily possible. The classes in question are intended to be extended, and I didn't want to impose any weird requirements on constructor parameters if the users want to have their own constructors. Even more complicated is that some of the information used for configuration isn't available to the user. I would have to make it available to the user and trust they will route it back to the proper classes during construction.

目前,我的解决方案是在这些方法前面加上一些前缀,并提醒用户不要调用带有该前缀的方法.这足够灵活",可以让我添加更多受限制的方法,但它仍然假设用户将遵循我的指示.这也不是最优雅的解决方案.

Currently, my solution is to prefix these methods with something and note to users not to call methods with said prefix. This is "flexible" enough to let me add more restricted methods, but it still makes the assumption that the user will follow my instructions. It's also not the most elegant of solutions.

我想知道是否有某种方法可以轻松限制这些方法.我想在其中添加一行来检查它是否是调用它们的中心类,但这似乎也不是最好的解决方案.

I was wondering if there was some way to restrict these methods easily. I thought about adding a line in them that checked if it was the central class calling them, but that doesn't seem like the best solution either.

我应该解释这个架构的目的.每个类在操作堆栈中执行特定任务.中心班级的工作是指挥一个班级执行它的任务,收集结果,并将结果分发给需要它们的其他班级.然后班级移动到下一个班级并做同样的事情.如何执行任务取决于扩展类.我想要限制的方法是有助于分散结果的方法.我希望这能让我的意图更加明确.

I should explain the purpose of this architecture. The classes each perform a particular task in a stack of operations. The job of the central class is to command one class to perform it's task, gather results, and disperse the results to the other classes that need them. Then the class moves to the next class and does the same thing. How the tasks are performed is up to the extended class. The methods I want restricted are the ones that aid in the dispersing of the results. I hope that makes my intentions more clear.

推荐答案

这完全取决于您的中心班级的角色.从您的描述来看,它似乎是某种假脱机程序或调度程序.如果假脱机对象实际上是由该中心类创建的,您可以通过将这些对象设为私有来向客户端隐藏这些对象,然后在中心对象中创建存根以允许客户端调用您允许的函数.

It all depends on the role of your central class. From your description it seems to be a spooler or scheduler of sorts. If the spooled objects are actually created by that central class, you can hide the objects from the client by making those objects private, and then create stubs in the central object that allows the client to call the functions that you do allow.

例如(概念代码,可能无法实际编译)

E.g. (conceptual code, might not actually compile)

class HiddenSubClass {
  public function MyFunc() {
    echo "hello";
  }
}

class CentralClass {
  private $obj;

  function __construct() {
     $obj=new HiddenSubClass();
  }

  function SubClassMyFunc () {
    $obj->MyFunc();
  }
}

客户端只有 CentralClass 对象,因此只能调用 SubClassMyFunc,而不能调用 MyFunc.这样做的缺点是,这也会对客户端隐藏库的面向对象结构,这可能是也可能不是.

The client only has the CentralClass object and hence can only call SubClassMyFunc, but not MyFunc. The downside of this is that this will also hide the object oriented structure of the library from the client, which may or may not be desireable.

然而,如果对象是由客户端创建的,然后交给你的中心类,那么你几乎无法阻止对象的所有者(客户端)随心所欲地使用它.客户端可以调用您的中心类可以调用的任何内容.然后,您可能需要将要保护的代码移出所述类 - 移至帮助类"或中央类本身.

If, however, the objects are created by the client and then handed over to your central class, there is very little you can do to prevent the owner of the object (the client) do with it whatever it wants. The client can call anything your central class can. You might then have to move the code you want to protect out of said class - either into "helper classes" or into the central class itself.

如果你确实想破解"它,这将是一种方法(虽然我不推荐它,因为它很臭):

If you do want to "hack" it, this would be one approach (although I don't recommend it, because it's quite smelly):

在你的中心班级有一个私人秘密".

Have a "private secret" in your central class.

class CentralClass {
  private $MySecret=42;

  function IsValidSecret($anumber) {
    return ($anumber==$this->MySecret);   
  } 
}

现在,当中心类在假脱机对象上调用某些受保护"函数时,它可以将 $MySecret 作为参数传递.如果 CentralClass 是单例,假脱机对象可以调用该单例来检查传递的秘密是否正确.如果 CentralClass 不是单例,则您还必须传递该对象.

Now the central class can pass $MySecret as parameter, when it calls certain "protected" function on the spooled objects. If CentralClass is a singleton, the spooled object can call that singleton to check if the passed secret is correct. If CentralClass is not a singleton, you'll have to pass that object as well.

class MySpooledObject {
  function SuperSecretFunction ($asecret) {
    if (!$CentralSingleton->isValidSecret($asecret)) { die(); }
  }

  function SuperSecretFunction2 ($acentralobject,$asecret) {
    if (!$acentralobject->isValidSecret($asecret)) { die(); }
  }
}

这有很多变体.中心类可以在调用对象的方法之前设置一个内部标志,并在它返回时重置它.在这种情况下,对象可以询问中央类是否设置了该标志,并且不需要传递任何秘密.但是,除非它是单例,否则您仍然需要 $acentralobject.

There are many variations of this. The central class could set an internal flag before calling the object's method, and reset that upon its return. In this scenario the object can ask the central class if that flag is set, and no secret needs to be passed. You'll still need the $acentralobject however, unless it is a singleton.

这篇关于PHP 限制调用公共方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆