【问题标题】:Implementing PHP code into a function将 PHP 代码实现到函数中
【发布时间】:2013-02-05 23:22:46
【问题描述】:

我有一些代码想做成一个函数,因为我想在我的代码的不同部分和不同的页面上使用它,而且我不想到处都有代码。我正在使用 PHPseclib 库和类。独立工作的代码是:

    set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');
include('phpseclib/Net/SSH2.php');

$ssh = new Net_SSH2('$address');
if (!$ssh->login('$username', '$password')) {
    exit('Login Failed');
}


$sPath = "minecraft/servers/";
$sSavingCode = "server.properties";
$motd = "test";

echo $ssh->exec("cat > $sPath$sSavingCode <<EOF
motd=".$motd."
EOF
");

我想把它变成一个函数,所以我尝试这样做:

set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib'); 包括('phpseclib/Net/SSH2.php');

 $ssh = new Net_SSH2('$address');
    if (!$ssh->login('$username', '$password')) {
        exit('Login Failed');
    }    
function test($motd)
    {
        $sPath = "minecraft/servers/";
        $sSavingCode = "server.properties";

        $ssh->exec("cat > $sPath$sSavingCode <<EOF
        motd=".$motd."
        EOF
        ");
    }

在函数之外和上面的其余代码。我正在尝试调用如下函数:

$motd = "Server";
test($motd);

但这会带来服务器错误。这个功能可以吗?还是应该在每次我想使用的时候把代码放在我需要的地方?

【问题讨论】:

  • 返回的错误是什么?
  • HTTP 错误 500(内部服务器错误):服务器尝试完成请求时遇到了意外情况。
  • 我认为这是一个未定义的错误,因为在你的函数中你没有定义的 $ssh 变量来调用 exec
  • 尝试 $GLOBALS['ssh'].exec 或在你的测试函数 global $ssh 中定义一个本地副本;
  • 问得好,因为我怀疑来自 JavaScript 的人期望 '$ssh' 可以从 test() 中自动访问。

标签: php function ssh phpseclib


【解决方案1】:

你的函数依赖于$ssh,所以它应该作为参数传递:

function test(Net_SSH2 $ssh, $motd)
{
    $sPath = "minecraft/servers/";
    $sSavingCode = "server.properties";

    $ssh->exec("cat > $sPath$sSavingCode <<EOF
    motd=".$motd."
    EOF
    ");
}

$ssh = new Net_SSH2('$address');
// ... 
test($ssh, $motd);

【讨论】:

  • 这是不是比在函数中使用global $ssh;更好的方法?
  • @Runner 使用全局变量是一种不好的编程习惯;如果您需要访问更多变量,最好将它们组合成对象并传递。
  • 这是一个很好的答案。考虑一下如果你想在不同的函数中使用原始的 $ssh 会发生什么,或者如果函数改变了怎么办?如果变量 $ssh 被其他人错误地设置为全局范围怎么办?此外,请考虑您可能希望使用相同的函数连接到不同的 $address 或使用相同的函数调用不同地址的场景。
  • 谢谢您,现在效果很好。我应该将 $ssh 放在函数所在的文件中,还是我调用函数的文件中?
  • @Runner 在哪里定义 $ssh 本身并不重要。它可以在传递给函数之前完全在其他地方创建。
【解决方案2】:

当您想要在函数之外定义的 $ssh 变量时,您正在访问本地 $ssh 变量。

所以在你的函数中声明 'global $ssh' 并使用它,否则你会尝试在未定义的本地调用 exec。

$ssh = new Net_SSH2('$address');
if (!$ssh->login('$username', '$password')) {
    exit('Login Failed');
}    
function test($motd)
{
    global $ssh;
    $sPath = "minecraft/servers/";
    $sSavingCode = "server.properties";

    $ssh->exec("cat > $sPath$sSavingCode <<EOF
    motd=".$motd."
    EOF
    ");
}

另外,预定义的 php 数组 $Globals 允许您在不定义本地变量的情况下访问这些变量

$ssh = new Net_SSH2('$address');
if (!$ssh->login('$username', '$password')) {
    exit('Login Failed');
}    
function test($motd)
{

    $sPath = "minecraft/servers/";
    $sSavingCode = "server.properties";

    $GLOBALS['ssh']->exec("cat > $sPath$sSavingCode <<EOF
    motd=".$motd."
    EOF
    ");
}

【讨论】:

  • 这个函数绝对应该在其参数中传递给它的 $ssh 变量。使其全局化会影响函数的可重用性。
  • 当然,不会影响我回答的有效性。不应该根据他想要如何实现它来否决一个完全有效的答案
  • 谢谢。这种方式很有效,但我想我会选择另一种方式,因为它似乎具有更多的功能和移动性。
猜你喜欢
  • 1970-01-01
  • 2011-05-06
  • 1970-01-01
  • 1970-01-01
  • 2021-12-11
  • 2018-01-20
  • 2017-07-02
  • 2017-06-02
  • 2021-09-20
相关资源
最近更新 更多