php致命错误:调用成员函数

时间:2013-12-12 03:19:39

标签: php function

我的代码是:

<?php

require('soap_config.php');

$client = new SoapClient(null, array('location' => $soap_location,
                                     'uri'      => $soap_uri,
                                     'trace' => 1,
                                     'exceptions' => 1));

try {
    if($session_id = $client->login($username,$password)) {
        echo 'Logged successfull. Session ID:'.$session_id.'<br />';
    }

    //* Set the function parameters.
    $client_id = 2;

    function ftpadduser()
    {

        $params = array(
                        'server_id' => 1,
                        'parent_domain_id' => 1,
                        'username' => 'threep',
                        'password' => 'wood',
                        'quota_size' => 10,
                        'active' => 'y',
                        'uid' => '5000',
                        'gid' => '5000',
                        'dir' => 'maybe',
                        'quota_files' => -1,
                        'ul_ratio' => -1,
                        'dl_ratio' => -1,
                        'ul_bandwidth' => -1,
                        'dl_bandwidth' => -1
                        );

        $affected_rows = $client->sites_ftp_user_add($session_id,$client_id, $params);

        echo "FTP User ID: ".$affected_rows."<br>";

    }

    ftpadduser();

    if($client->logout($session_id)) {
        echo 'Logged out.<br />';
    }


} catch (SoapFault $e) {
    echo $client->__getLastResponse();
    die('SOAP Error: '.$e->getMessage());
  }

?>

虽然如果我不以某种方式使用函数我可以运行所有内容在这种情况下它给了我

Logged successfull. Session ID:9a46b41e293ff9d5d5604648013caf76

Fatal error: Call to a member function sites_ftp_user_add() on a non-object in C:\xampp\htdocs\img\client_get.php on line 39

作为输出,我想知道是否有人可以帮助我如何运行此功能并避免致命错误。

由于

1 个答案:

答案 0 :(得分:6)

除非您明确地将它们声明为global,否则对函数内部变量的引用是本地的。

您已初始化$ client,但未将其传递到您的ftpadduser()函数中,因此您的方法调用看不到正确的值并失败。

您还需要其他尚未传入的变量。请按照以下方式解除您的功能:

function ftpadduser($client, $session_id, $client_id) {
    //your code
 }

然后使用ftpadduser($client, $session_id, $client_id);

进行调用