我可以在 PHP 类中包含代码吗? [英] Can I include code into a PHP class?

查看:35
本文介绍了我可以在 PHP 类中包含代码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个 PHP 类,比如说 Myclass.php.现在在该类中,我只想定义类本身和一些实例变量.但所有方法都必须来自 Myclass_methods.php 文件.我可以将该文件包含在类主体中吗?

I want to make a PHP class, lets say Myclass.php. Now inside that class I want to define just the class itself and some instance variables. But all the methods must come from a Myclass_methods.php file. Can I just include that file into the class body?

我有充分的理由想把它分开.简而言之,我将有一个后端,我可以在其中更改类的业务逻辑,而所有其他事情必须保持不变.系统为我维护所有的 ORM 和其他东西.

I have good reasons why I want to seperate this. In short, I'll have a backend in which I can change the business logic of a class, while all other things must remain untouched. The system maintains all the ORM and other stuff for me.

但如果这是一个坏主意,最好在编辑业务逻辑后重新生成整个类文件(因此,在本例中为用户定义的方法).

But if this is a bad idea, it might be better to re-generate the whole class file after editing the business logic (so, the user-defined methods in this case).

性能问题:如果在一个请求中 Myclass.php 只包含一次,实际上 Myclass_methods.php 也应该只包含一次.可能是错的.专家?

Performance question: If during one request Myclass.php is included just once, actually that Myclass_methods.php should also be included just once. Might be wrong. Experts?

推荐答案

没有.您不能在类正文中包含文件.
在定义类的文件中,您只能在方法主体类主体外中包含文件.

根据你的描述,我认为你想要这个:

From your description I take you want this:

<?php // MyClass.php
class MyClass
{
    protected $_prop;
    include 'myclass-methods.php';
}

<?php // myclass-methods.php
public function myMethod()
{
   $this->$_prop = 1;
}

运行此代码将导致

Parse error: syntax error, unexpected T_INCLUDE, expecting T_FUNCTION

这是可能的

<?php // MyClass.php
class MyClass
{
    protected $_prop;
    public function __construct() // or any other method
    {
        include 'some-functions.php';
        foo($b); // echoes 'a';
    }
}

<?php // some-functions.php
$b = 'a';
function foo($str)
{
   echo $str;
}

这样做,会将包含文件的内容导入方法范围,而不是类范围.您可以在包含文件中包含函数和变量,但不能包含方法.您可以但不应该将整个脚本也放入其中并更改方法的作用,例如

Doing it this way, will import the contents of the include file into the method scope, not the class scope. You may include functions and variables in the include file, but not methods. You could but should not put entire scripts into it as well and change what the method does, e.g.

<?php // MyClass.php
    // ...
    public function __construct($someCondition)
    {
        // No No Code here
        include ($someCondition === 'whatever') ? 'whatever.php' : 'default.php';
    }
    // ...

<?php // whatever.php
    echo 'whatever';

<?php // default.php
    echo 'foo';

但是,以这种方式修补类以展示不同的行为并不是您在 OOP 中应该这样做的方式.这完全是错误的,应该让你的眼睛流血.

However, patching the class this way to exhibit different behavior is not how you should do it in OOP. It's just plain wrong and should make your eyes bleed.

因为你想动态改变行为,扩展类也不是一个好的选择(见下文为什么).您真正想要做的是编写一个 interface 并使您的类使用实现此接口的对象,从而确保适当的方法可用.这称为策略模式,其工作方式如下:

Since you want to dynamically change behavior, extending the class is also not a good option (see below why). What you really will want to do is write an interface and make your class use objects implementing this interface, thus making sure the appropriate methods are available. This is called a Strategy Pattern and works like this:

<?php // Meowing.php 
interface Meowing
{
    public function meow();
}

现在你得到了所有喵喵行为都必须遵守的契约,即拥有喵喵方法.接下来定义喵喵行为:

Now you got the contract that all Meowing Behaviors must obey, namely having a meow method. Next define a Meowing Behavior:

<?php // RegularMeow.php
class RegularMeow implements Meowing
{
    public function meow()
    {
        return 'meow';
    }
}

现在要使用它,请使用:

Now to use it, use:

<?php // Cat.php
class Cat
{
    protected $_meowing;

    public function setMeowing(Meowing $meowing)
    {
        $this->_meowing = $meowing;
    }

    public function meow()
    {
        $this->_meowing->meow()
    }
}

通过将 Meowing TypeHint 添加到 setMeowing,您可以确保传递的参数实现了 Meowing 接口.让我们定义另一个喵喵行为:

By adding the Meowing TypeHint to setMeowing, you make sure that the passed param implements the Meowing interface. Let's define another Meowing Behavior:

<?php // LolkatMeow.php
class LolkatMeow implements Meowing
{
    public function meow()
    {
        return 'lolz xD';
    }
}

现在,您可以轻松地交换如下行为:

Now, you can easily interchange behaviors like this:

<?php
require_once 'Meowing.php';
require_once 'RegularMeow.php';
require_once 'LolkatMeow.php';
require_once 'Cat.php';

$cat = new Cat;
$cat->setMeowing(new RegularMeow);
echo $cat->meow; // outputs 'meow';
// now to change the behavior
$cat->setMeowing(new LolkatMeow);
echo $cat->meow; // outputs 'lolz xD';

虽然您也可以通过定义继承解决上述问题一个 abstract BaseCat 和 meow 方法,然后从中派生出具体的 RegularCat 和 Lolkat 类那,你必须考虑你想要达到的目标.如果您的猫永远不会改变它们喵喵叫的方式,请继续使用继承,但如果您的 RegularCat 和 Lolkat 应该能够任意喵喵叫,那么请使用 Strategy 模式.

While you also could have solved the above with inheritance by defining an abstract BaseCat and meow method and then deriving concrete RegularCat and Lolkat classes from that, you have to consider what you want to achieve. If your cats will never change the way they meow, go ahead and use inheritance, but if your RegularCat and Lolkat is supposed to be able to do arbitrary meows, then use the Strategy pattern.

有关 PHP 中的更多设计模式,请查看以下资源:

For more design patterns in PHP, check these resources:

这篇关于我可以在 PHP 类中包含代码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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