Hi again ppl.

I was just merryily adding include_once statements for all my classes in my php file,
basically adding ALL the classes as include statements in all my worker files.

Is this going to cause any kind of performance issue, does the php parser parse every single file before execution of the script?

this is the pattern im using:

worker.php

include_once("all.classes.php");

all.classes.php

include_once("object1.class.php");
include_once("object2.class.php");
include_once("object3.class.php");
include_once("object4.class.php");

and so on. sometimes there will be as many as 40 includes in a script.

any advise welcome,

thank again,

simon

    well, if you really need 40 classes in your script it's ok i guess

    but i can't imagine why one would need 40 classes at all and why he would need all of them in one single script

      but i can't imagine why one would need 40 classes at all

      Not quite sure what sort of OO programming you have done before, I have worked on enterprise apps with > 12,000 classes / objects kicking around - perhaps you have been doing that "I group functions by namespace" thing - therefore I understand OO!

      Please note, the question was not one of semantics. therefore could someone comment on the question, perhaps it was not well defined; ill try again:

      How do include statements determentally affect the running of a php script?

      thanks again,
      simon

        Originally posted by simon622
        Not quite sure what sort of OO programming you have done before, I have worked on enterprise apps with > 12,000 classes / objects kicking around

        12,000 objects != 12,000 classes

        perhaps you have been doing that "I group functions by namespace" thing

        No 😉
        I do the "I group functions by the object they're representing and try to keep the resulting classes as generalised as possible and as specialised as neccessary thing" (at least I think so 🙂)

        How do include statements determentally affect the running of a php script?

        if you include a file all code within that file is being executed

        if you have no code within the included file but within the class defined in the included file php will recognize that a new class is being introduced and that's it (at least for php4)

        i'm not sure but i could imagine that php5 checks the visibility and accessibility of class attributes and methods as well

          ... you can use a php accelerator to eliminate a lot of these concerns. It will load the pages/includefiles into cache and only reload as necessary.

          Zend do one, but there is also a free one the name of which escapes me.

            Right. I am not including executable code per ce', merely classes that can then be accessed.
            So we're saying that only the class/method signitures are reflected? this is good i guess. Im just slightly concerned that having (lets be obtuse for the sake of the example):

            100 included classes in 1 file - at some point during the request I guess this results in 100 IO operations (at the lowest level of granularity). Im not sure Im comfortable with this for every request, especially on a heavily loaded system. Need to think about things like ulimit and file descriptors I guess??

            As a thought, would it not therefore be better to include the 100 classes (although I may get accused of thinking the multiplicity is 1 to 1 😉 - so therefore 100 files) all in one bug mother of a file. Even though this may well exceed like 600k?

            Therefore only 1 IO operation is performed, but this wil burn out the memory quickly i guess, as it will all be read at once.

            Im trying to seek where exactly the trade off is here.

            more comment welcome.

              Originally posted by mrhappiness
              if you include a file all code within that file is being executed

              Actually no, when you include a file the code gets parsed, not executed. Any code outside of a function or class declaration would get executed.

              In answer - yes including files will have noticeable effect on the speed of your program. 40 includes means 40 file reads and maybe alot of parsing. It could mount up. PHP 5 offers the __autoload() special function which loads (class) files in only when you try to instatiate the class. This can be imitated in PHP 4 but it's not nearly so good, and requires that you call a wrapper class directly.

              class ClassCaller
              {
              	function &call($classname, $args = array()) 
              	{ 
               		if(class_exists($classname)) 
              	 	{
              			return new $classname(implode(',', $args)); // Assumes $args is array
              		} 
              		elseif(file_exists($file = $classdir . "/" . $classname. ".php")) 
              		{
              
              		require_once($file); 
              		return new $classname($args); 
              	} 
              	else 
              	{ 
              		die("Call to undefined class $classname()"); 
              	} 
              }
              }
              
              class MyTestClass
              {
              	function myTestClass($string)
              	{
              		echo $string;
              	}
              }
              
              $obj =& ClassCaller::call('MyTestClass', array('test')); // This replaces the usual 'new ObjName($args);'
              

              Also this assumes that you want to create a new object, and that each class lives in it's own file, and the filename is the same as the classname (a limitation of __autoload() in any case).

                Originally posted by Shrike
                Actually no, when you include a file the code gets parsed, not executed. Any code outside of a function or class declaration would get executed.

                taht's what a i meant, sorry for expressing myself so bad, thought it was clear after reading the next sentence

                  Thanks everyone for the great posts, this will give me a lot to chew over and think about. am especially interested in include on instantiation pattern. May have to have a look into this further.

                  thanks again,

                  SImon

                    I've just realised that imploding the $args array in an attempt to make a parameter list doesn't work. Got me a bit stumped, that one.

                      the one you were looking for was explode i think!

                        No explode creates an array from a string based on a given delimiter (and implode does the opposite). My idea was to create the parameter list by implod()-ing the array on commas - but PHP treats this as one string and so it gets passed as the first parameter (and chucks a wobbly if any other parameters are required). It might be possible with some eval() trickery, I'm not sure though.

                          Write a Reply...