16

Possible Duplicate:
Is there a way to program 100% object-oriented in PHP?

What I am trying to accomplish would look something like this:

 <?php
      //index.php
      class Site{
           public static function main(){
                // starts all the processing of the site
           }
      }
 ?>

So that when someone visits the site's index.php the site will start up without having any code outside the class.

Yes I know that I could use __autoload or even spl_autoload_register to handle autoloading of classes but that would still most likely need to be added outside of the class.

I have my doubts that this is possible but I don't know why this wouldn't be possible.

4

4 回答 4

31

No.

Java's entry point is defined as the main method. PHP's entry point is defined as the first line in the first file that gets executed. You will have to start with "procedural" code.

于 2012-09-04T10:34:51.920 回答
1

You can write your Site class like that and make that class construct everything, but you'd still need an entry file to initialize the class. You could 'boot up' your site with one line in your index file. Doesn't seem like a big inhibitor of flexibility.

So, answer would be no :)

于 2012-09-04T10:34:58.680 回答
0

There is no such concept out of the box with php. You will have to have something either in index.php or something in an .htaccess file that will redirect the user to the file you want, but the class will always have to be instanced by code

于 2012-09-04T10:36:45.470 回答
-1

You can achieve this very easy bij using a .htaccess on you site:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

Your index.php wil then do al the processing. With 1 line of procedural code, you could create a class, and move on from there.

<?php
      //index.php
      class Site{
           public static function main(){
                // starts all the processing of the site
           }
      }

      Site::main()
 ?>

See my tutorial/blog.

于 2012-09-04T10:39:12.633 回答