PHP是否正确设置面向对象类?

PHP是否正确设置面向对象类?,php,oop,Php,Oop,我被告知必须创建一个config.php文件来保存所有类 例如,假设我有3门课 一级:住宅 第二类:myCar 三级公路:公路 在config.php中我称之为: /** * Index.php **/ include("class/house.class.php"); $class = new House(); echo $class->echoMe("hey"); 然后我在每个页面中都包含config.php文件,例如index.php 但现在,我想延长班级道路。 我不能在那

我被告知必须创建一个config.php文件来保存所有类

例如,假设我有3门课

一级:住宅

第二类:myCar

三级公路:公路

在config.php中我称之为:

/**
 * Index.php
 **/
include("class/house.class.php");

$class = new House();

echo $class->echoMe("hey");
然后我在每个页面中都包含config.php文件,例如index.php

但现在,我想延长班级道路。

我不能在那个类中包含config.php?或者在config.php中包含另一个类文件时将其包含在其中

这是错误的课程设置方式吗?

如何扩展类而不出错。

是否每次都必须创建新对象?

例如:

我将这样做,而不是包含config.php:

/**
 * Index.php
 **/
include("class/house.class.php");

$class = new House();

echo $class->echoMe("hey");
在每个文件中,我都会创建新对象,这样我就可以在需要时扩展一些特定的类了?

function autoload($class) {
    if (is_file($file = 'www/content/includes/class/'.$class.'.php')) {
        require_once($file);
    }
}

spl_autoload_register('autoload');
在config.php文件中包含以下代码。确保在要加载类的每个文件中都包含配置文件

确保该文件与调用的类具有相同的名称。例如

$house = new House();
将要求类文件名为House.php

<?
    class House {

        function something() {

        }

    }

在config.php文件中包含以下代码。确保在要加载类的每个文件中都包含配置文件

确保该文件与调用的类具有相同的名称。例如

$house = new House();
将要求类文件名为House.php

<?
    class House {

        function something() {

        }

    }
基本上,您可以使用创建一个函数,每次调用PHP以前没有遇到过的类时都会调用该函数

/**
* config.php
*/
function classAutoLoad($class) {
    if (file_exists("class/$class.class.php"))
        include("class/$class.class.php");
}

spl_autoload_register('classAutoload');

/**
* someFile.php
*/
reauire_once('config.php');
$house = new House();
基本上,您可以使用创建一个函数,每次调用PHP以前没有遇到过的类时都会调用该函数

/**
* config.php
*/
function classAutoLoad($class) {
    if (file_exists("class/$class.class.php"))
        include("class/$class.class.php");
}

spl_autoload_register('classAutoload');

/**
* someFile.php
*/
reauire_once('config.php');
$house = new House();

假设我想处理登录,登录名位于users.class.php中。我可以在login.php中包含users.class.php并执行$users=new users();?这是正确的方法吗?非常感谢,我现在明白了。。所以现在我只需要在login.php中包含配置,只需执行$users=newusers()?对只要确保类文件夹的路径正确,并且php文件名为Users.php,您就可以了:)假设我想处理login,并且login位于Users.class.php中。我可以在login.php中包含users.class.php并执行$users=new users();?这是正确的方法吗?非常感谢,我现在明白了。。所以现在我只需要在login.php中包含配置,只需执行$users=newusers()?对只要确保类文件夹的路径正确,并且php文件名为Users.php,您就可以了:)