2015-07-03 72 views
-1

如果我有一个叫做Helpers.php的函数,并且函数的功能是someFunction(),如何在不使用范围解析运算符的情况下从不同的类调用该函数?没有范围解析运算符的调用函数

这是我目前等级:

<?php 

class SomeClass 
{ 
    public function helloWorld() 
    { 
     return Helpers::someFunction(); 
    } 
} 

我想,而只返回someFunction();。我怎样才能做到这一点?

回答

0

您正在寻找global functions。自从您使用Laravel以来,不需要在类中声明辅助函数,而是将它们声明为普通的PHP文件并includerequire它们在您的文件routes.phpbootstrap/start.php文件中。

实施例:

routes.php文件

<?php 

include 'helpers.php'; 

helpers.php

<?php 

function helloWorld() 
{ 
    return 'test'; 
} 

在控制器:

class WelcomeController extends Controller 
{ 
    public function index() 
    { 
     echo helloWorld(); 
    } 
} 
0

可以在一个文件作为声明全局函数助手功能,例如mple,您可以创建一个文件如下app/Helpers/functions.php,只是声明函数是这样的:

<?php 

// app/Helpers/Functions.php 

someFunction() 
{ 
    // ... 
} 

SomeAnotherFunction($arg1, $arg2) 
{ 
    // ... 
} 

要使用这些功能,您可以直接拨打他们的任何地方,如:

someFunction(); 

SomeAnotherFunction('something', 'SomeThingElse'); 

只要确保添加在您的“自动加载”部分中输入composer.json这样的文件:

"autoload": { 
    "classmap": [ 
     "database" 
    ], 
    "psr-4": { 
     "App\\": "app/" 
    }, 
    "files": [ 
     "app/Helpers/functions.php" // <--- This is required 
    ] 
}, 
相关问题