PHP封装没有类?

发布于 2024-11-28 12:48:13 字数 458 浏览 1 评论 0原文

是否可以在 PHP 中封装变量或函数而不将它们包装在类中?我所做的是:

//Include the file containing the class which contains the variable or function
include('SomePage.php');

//Instantiate the class from "SomePage.php"
$NewObject = new SomeClassFromSomePage();

//Use the function or variable
echo $NewObject->SomeFuncFromSomeClass();
echo $NewObject->SomeVarFromSomeClass;

我的目的是避免命名冲突。这个例行公事虽然有效,但让我很累。如果我不能在没有类的情况下做到这一点,是否可以不实例化类?然后立即使用变量或函数?

Is it possible to encapsulate, a variable or function let say, in PHP without wrapping them in a class? What I was doing is:

//Include the file containing the class which contains the variable or function
include('SomePage.php');

//Instantiate the class from "SomePage.php"
$NewObject = new SomeClassFromSomePage();

//Use the function or variable
echo $NewObject->SomeFuncFromSomeClass();
echo $NewObject->SomeVarFromSomeClass;

My intention is to avoid naming conflict. This routine, although it works, makes me tired. If I cannot do it without class, it is possible not to instantiate a class? and just use the variable or function instantly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一个人的夜不怕黑 2024-12-05 12:48:14

PHP 命名空间 的目的是归档完全相同的目标:

<?php // foo.php
namespace Foo;
function bar() {}
class baz {
    static $qux;
}
?>

当使用像这样调用命名空间函数:

<?php //bar.php
include 'foo.php';
Foo\bar();
Foo\baz::$qux = 1;
?>

PHP Namespaces were made to archive the exact same goal:

<?php // foo.php
namespace Foo;
function bar() {}
class baz {
    static $qux;
}
?>

When using call namespaced functions like this:

<?php //bar.php
include 'foo.php';
Foo\bar();
Foo\baz::$qux = 1;
?>
留一抹残留的笑 2024-12-05 12:48:14

这是一种无需类封装的方法

<?php

(function (){

$xyz = 'XYZ';

})();

echo $xyz; // warning: undefined

封装替代方案

使用此方法,您可以最大限度地减少无意使用数组键(使用它而不是变量)。分配后还可以在任何地方使用存储在数组中的值。数组键区长度较短,键内可变,封装函数内部;在封装函数之外,变量可以在键中使用,但也可以在长描述性键中使用。也可以使用嵌套封装。

例子

<?php

define('APP', 'woi49f25gtx');

(function () {

    $pre = 'functions__math__'; // "functions" is main category, "math" is sub.

    $GLOBALS[APP][$pre . 'allowedNumbers'] = [3,5,6];

    $GLOBALS[APP][$pre . 'square'] = function ($num) {
        return $num * $num;
    };

    $GLOBALS[APP][$pre . 'myMathFunction'] = function ($num) use ($pre) {
        if(in_array($num,$GLOBALS[APP][$pre . 'allowedNumbers'])) return 'not allowed';
        return $GLOBALS[APP][$pre . 'square']($num);
    };

})();

echo $GLOBALS[APP]['functions__math__myMathFunction'](4);

This is a way to encapsulate without Class

<?php

(function (){

$xyz = 'XYZ';

})();

echo $xyz; // warning: undefined

Encapsulation Alternative

With this method you can minimize unintentional using array key(uses it instead of variables). Can also use value stored in array anywhere after assigning. Shorter array key area length with variable in keys, inside encapsulation function; outside encapsulation function, variables can be used in keys but otherwise long discriptive keys. Nested encapsulation can also be used.

Example

<?php

define('APP', 'woi49f25gtx');

(function () {

    $pre = 'functions__math__'; // "functions" is main category, "math" is sub.

    $GLOBALS[APP][$pre . 'allowedNumbers'] = [3,5,6];

    $GLOBALS[APP][$pre . 'square'] = function ($num) {
        return $num * $num;
    };

    $GLOBALS[APP][$pre . 'myMathFunction'] = function ($num) use ($pre) {
        if(in_array($num,$GLOBALS[APP][$pre . 'allowedNumbers'])) return 'not allowed';
        return $GLOBALS[APP][$pre . 'square']($num);
    };

})();

echo $GLOBALS[APP]['functions__math__myMathFunction'](4);
往事风中埋 2024-12-05 12:48:13

要使用类方法和变量而不实例化,必须将它们声明为静态

class My_Class
{ 
    public static $var = 123;

    public static function getVar() {
     return self::var;
    }
}


// Call as:
My_Class::getVar();

// or access the variable directly:
My_Class::$var;

在 PHP 5.3 中,您还可以使用 命名空间

namespace YourNamespace;

function yourFunction() {
  // do something...
}


// While in the same namespace, call as 
yourFunction();

// From a different namespace, call as
YourNamespace\yourFunction();

To use class methods and variables without instantiating, they must be declared static:

class My_Class
{ 
    public static $var = 123;

    public static function getVar() {
     return self::var;
    }
}


// Call as:
My_Class::getVar();

// or access the variable directly:
My_Class::$var;

With PHP 5.3, you can also use namespaces

namespace YourNamespace;

function yourFunction() {
  // do something...
}


// While in the same namespace, call as 
yourFunction();

// From a different namespace, call as
YourNamespace\yourFunction();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文