简体   繁体   English

如何使用PHP在另一个类中调用方法

[英]How to call method within another class using PHP

I created two php classes separately. 我分别创建了两个php类。 those are Student.php and Main.php this is my code. 这些是Student.php和Main.php,这是我的代码。

this is my Student.php 这是我的Student.php

<?php

class Student {

private $name;
private $age;

function __construct( $name, $age) {
    $this->name = $name;
    $this->age = $age;
}

function setName($name){
    $this->name = $name;
}

function setAge($age){
    $this->age = $age;
}

function getName() {
    return $this->name;
}

function getAge() {
    $this->age;
}

function display1() {
    return "My name is ".$this->name." and age is ".$this->age;
}

}

?>

this is my Main.php 这是我的Main.php

<?php

class Main{

function show() {
    $obj =new Student("mssb", "24");
    echo "Print :".$obj->display1();
}

}

$ob = new Main();
$ob->show();

?>

so my problem is when I call taht show method it gives Fatal error: Class 'Student' not found what is the wrong in here. 所以我的问题是,当我调用taht show方法时,它出现致命错误:类'Student'在这里找不到错误。 is it necessary to import or something? 有必要进口还是什么? please help me. 请帮我。

The PHPUnit documentation says used to say to include/require PHPUnit/Framework.php, as follows: PHPUnit文档说过去曾说要包含/要求PHPUnit / Framework.php,如下所示:

require_once ('Student.php');

As of PHPUnit 3.5, there is a built-in autoloader class that will handle this for you: 从PHPUnit 3.5开始,有一个内置的autoloader类将为您处理此问题:

require_once 'PHPUnit/Autoload.php'

add

require_once('Student.php') 

in your Main.php-file (on top) or before the inclusion of any other file... 在您的Main.php文件中(在顶部)或在包含任何其他文件之前...

You can use require_once('Student.php') or you can use PHP5 new feature namespaces for that. 您可以使用require_once('Student.php')或为此使用PHP5新功能名称空间 For a example assume your Student.php is in a dir called student. 例如,假设您的Student.php位于名为Student的目录中。 Then as the first line of Student.php you can put 然后,作为Student.php的第一行,您可以

<?php    
namespace student;

class Student {
}

Then in your Main.php 然后在您的Main.php中

<?php    
use student\Student;

class Main {
}

It's worth taking a look at the PSRs. 值得一看PSR。 Particularly PSR-1 特别是PSR-1

One of the recommendations is that 建议之一是

Files SHOULD either declare symbols (classes, functions, constants, etc.) or cause side-effects (eg generate output, change .ini settings, etc.) but SHOULD NOT do both 文件应该声明符号(类,函数,常量等)或引起副作用(例如,生成输出,更改.ini设置等),但不应两者都做

Following this guideline will hopefully make issues like the one you're having less common. 遵循该指南将有望使您遇到的问题不再那么常见。

For example, it's common to have one file that is just in charge of loading all necessary class files (most commonly through autoloading ). 例如,通常只有一个文件负责加载所有必要的类文件(最常见的是通过autoloading )。

When a script initializes, one of the first things it should do is to include the file in charge of loading all necessary classes. 脚本初始化时,应做的第一件事就是包括负责加载所有必要类的文件。

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

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