简体   繁体   English

另一个类中包含的类未声明为PHP

[英]Included class inside another class is not declared PHP

I have the following __construct of a selfmade class; 我有以下____自制类的构造;

public function __construct($ip, $user, $pass, $product) {
    $this->_ip = $ip;
    $this->_user = $user;
    $this->_pass = $pass;
    $this->_product = $product;

    $this->ssh = new Net_SSH2($this->_ip);
    if (!$this->ssh->login($this->_user, $this->_pass)) {
        return 'Login Failed';
    }
    $this->sftp = new Net_SFTP($this->_ip);
    if (!$this->sftp->login($this->_user, $this->_pass)) {
        return 'Login Failed';
    }
}

Now the problem is that it says Net_SSH2 and Net_SFTP is not declared, but I have included those classes at the page, im not sure, but can it be that I have to pass those classes into this class instead of just calling them? 现在的问题是,它说没有声明Net_SSH2和Net_SFTP,但是我不确定在页面中包括了那些类,但是我是否必须将这些类传递给该类而不是仅仅调用它们? If yes, how do I do that? 如果是,我该怎么做?

A better solution is to use autoload. 更好的解决方案是使用自动加载。

function __autoload($class_name) {
    require_once $class_name . '.php';
}

Now, when you ask for a new class, it will be auto loaded. 现在,当您要求一个新类时,它将被自动加载。

Try this. 尝试这个。

Have you included those classes using a require , include or an autoload function? 您是否使用requireinclude或autoload函数包含了这些类?

If you haven't, make sure the files in which those classes are defined are loaded. 如果还没有,请确保已加载定义了这些类的文件。

Now, we need to check for the namespace. 现在,我们需要检查名称空间。

At the top of the Net_SFTP , is there a namespace [SOMETHING] ? Net_SFTP的顶部,是否有namespace [SOMETHING] If there is, you must refer to the fully qualified class name, or use this class. 如果存在,则必须引用完全限定的类名称,或use此类。

An example: 一个例子:

<?php

namespace VendorName\BundleName;

class Net_SFTP { 
    ....
}

?>

Now, in order to use this class, we must do one of the following: 现在,为了使用此类,我们必须执行以下一项操作:

<?php

use VendorName\BundleName\Net_SFTP;

...
$this->ssh = new Net_SFTP(...);
...
?>

Or, directly: 或者,直接:

<?php

...
$this->ssh = new VendorName\BundleName\Net_SFTP(...);
...

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM