문제

내 이해는 "name".class.php가 PHP에서 클래스를 만들기위한 표준 규칙이며 결과적으로 각 클래스에 대해 별도의 파일이 있습니다.내가하고 싶은 것은 작은 PHP 클래스 계층 구조를 하나의 단일 파일로 통합하는 것입니다 ... consolidated.class.php를 말하십시오.

이렇게하면 서버에 업로드 / 다운로드가 쉽습니다. 단순 텍스트 검색을 실행 해야하는 경우 더 많은 파일을 보지 않아도됩니다.또한 필수적으로 수동으로 수동으로 삽입하기 때문에 포함 / 요구 사항을 제거 할 수 있습니다.

문제는 공연을 취하는 것과 통합 할 수 있습니까?

편집 / 업데이트

나중에, 나는 별도의 파일과 오토로더를 사용하고 있습니다.이것은 더 효율적이지 않지만 물론 파일의 크기에 따라 얼마만큼 의존해야합니다.

도움이 되었습니까?

해결책

The reason people use the one class per file convention is for class autoloaders. This allows your script to automatically only load code that is actually being used by your script in that particular instance. If you load ALL of you classes every time a page loads, it's a fairly inneficient way to go.

For more information about class autoloaders:

http://php.net/manual/en/language.oop5.autoload.php

http://php.net/manual/en/function.spl-autoload-register.php

다른 팁

I believe it depends on the size of the project. You can start small, with everything in just a single file, then, as the code accumulates and you get a better understanding of the domain you're modelling, you can extract units into separate files.

The convention to use a class per file comes from the Java world. For years, PHP has tried to imitate Java, however, some of the practices are absurd. Like, you can't have functions, just classes, even if they're nothing more than a collection of static methods.

Here's an example of a web framework, called Konstrukt, that uses multiple units (classes/functions/constants) per file and seems to be doing well.

As @Nabeel said, you may hit a point where you'd have problems locating code if everything is inside a single big file. Judge by the project you're developing.

Regarding performance, you'd have to benchmark, as once again it depends on the project, but require/include incur some performance overhead due to file I/O. So it might be better that you have a single file loaded per request. The actual parsing might be faster than the system calls that look for the file to require.

I would advise you not to do this because everything a page gets viewed, every single one of your classes is loaded. When using only a few classes which are loaded everytime, it does indeed make no difference performance-wise, but if you have quite a lot of classes and you only use a handful, depending on the page being loaded, you add a huge overload to the page.

I don't want to say the same things like others (which is actually correct ^^) to bring u some new ideas.

I would create few functions to include/require my class files.

Example:

you have 2 php-files:

1: login.php

2: register.php

<?php
   // login.php
   include '../system/functions/myFunctions.php';  
   loadMyClasses(1);
?>

<?php
  // register.php
  include '../system/functions/myFunctions.php';
  loadMyClasses(2); 
?>

<?php
  //myFunctions.php

  DEFINE("CLASS_PATH", "../system/class/");      

  function loadMyClasses($type_id)
  {  
     //Example: Load in everytime. These files are always required.
     require_once CLASS_PATH.'class.database.php';
     require_once CLASS_PATH.'class.sql.php';

     if($type_id == 1)
     {
           require(CLASS_PATH.'class.security.php');
           require(CLASS_PATH.'class.cryptor.php');   
           require(CLASS_PATH.'class.login.php');   
     }

     if($type_id == 2)
     {
           require(CLASS_PATH.'class.security.php'); 
           require(CLASS_PATH.'class.register.php');  
     }
  }
?>

And this way you can simply change the path of the class or interface files. You have to make changes only in one file.

I Hope, I was able to help a bit!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top