[PHP] Re: Includes eating up my time
Consider using the __autoload() function in php. (also the SPL autoloading features). It's a way to tell php to load a file when it cannot find a definition for a CLASS (only for classes). I don't know how you have set-uped your project, but in mine the only place in the project where I use include is thie __autoload() function. In other words - php supports load-on-demand for classes. Check out http://php.net/__autoload Regards, Emil Ivanov "Dave M G" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > PHP general list, > > This is probably obvious to people who are good at PHP, but I'm > > I have a PHP based CMS (content management system) built, which has grown > and become quite robust. It's now spread out over about 30 files, and each > file represents one class within the object oriented design. Each are a > couple hundred lines of code, and two or three of the most critical > classes are over a thousand lines of code. > > While first building it, I didn't really anticipate quite that many files. > What I did is have a file called "includes.php", which list all the files > to be included. Then I in turn included "includes.php" at the beginning of > my "index.php" file. Every page request passes through the "index.php" > file, so that basically means every single file is included at the start > of every new page request. > > I'm using Zend Studio, which has a "profile" option, which shows how long > it takes for my PHP scripts to complete a request. It has a breakdown > showing percentages of which scripts are using that processing time. > > Currently, my processes are taking under a second, but they can be around > half a second or more. Although it all happens too fast for me to really > notice as a person, it seems to me that a half second of processing time > might be kind of long and lead to scalability problems. > > My first question is: Is a half second too long? I'm pretty sure it is, > but maybe I'm just being paranoid. What do people consider to be > acceptable time frames for processing a web page similar to what Wikipedia > delivers? > > Most of the time is taken with the includes. Anywhere from 60% to 90% of > the time it takes to process my scripts is coming from the includes.php > file. > > I read somewhere that it's not a good idea to have more than 10 includes > in any one place. I'm fine with trying to break up my include requests, > but I'm unsure as to how. As each function in each class passes around > objects, it's not clear from looking at the code which ones are used at > any one time, so I'm unsure how to efficiently include only the necessary > classes. > > My second question is: Is there a systematic way of determining how to > incrementally include files that people use? Or is it just a constant > process of testing and checking? > > Thank you for any advice. > > -- > Dave M G > Ubuntu Feisty 7.04 > Kernel 2.6.20-16-386 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: includes and classes
Jason Barnett wrote: Dubreuilmedia wrote: Hi I was wondering about includes and classes. I have a class in which depending on a member variable, i should load the proper include, which is really the proper config file for that moment. How does a member variable control this? E.g. you set the member variable in the constructor and then include some config file? Now if i do this in the constructor i can't seem to be able to use the variables found in my config in the other member functions. Yeah, this can be a little confusing. When you include a file in a function, it only gains the scope of that function. I.e. variables in the included file are only available to that function. What you can do, however, is to assign the config variables to an array in the object. class someClass { function someClass($param) { // Include the config file if (21 == $param) { include_once 'config21.php'; } // Bring the config variables into this object foreach ($config as $varname => $value) { $this->config['varname'] = $value; } } } ?> Obviously the above method is a little messy. A better way to handle this would be object overloading with __set() and __get(), which is available in PHP4 with a few commands and PHP5 uses it by default. http://www.php.net/overload (I wouldn't be too afraid of the experimental warning, overloading in PHP5 is pretty much the same as in PHP4, and looks like it's here to stay). Or in your config.php use $GLOBALS['varName'] = 'value'; That would clear the whole thing up without extra storing in the object. -- paperCrane -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: includes and classes
Dubreuilmedia wrote: Hi I was wondering about includes and classes. I have a class in which depending on a member variable, i should load the proper include, which is really the proper config file for that moment. How does a member variable control this? E.g. you set the member variable in the constructor and then include some config file? Now if i do this in the constructor i can't seem to be able to use the variables found in my config in the other member functions. Yeah, this can be a little confusing. When you include a file in a function, it only gains the scope of that function. I.e. variables in the included file are only available to that function. What you can do, however, is to assign the config variables to an array in the object. class someClass { function someClass($param) { // Include the config file if (21 == $param) { include_once 'config21.php'; } // Bring the config variables into this object foreach ($config as $varname => $value) { $this->config['varname'] = $value; } } } ?> Obviously the above method is a little messy. A better way to handle this would be object overloading with __set() and __get(), which is available in PHP4 with a few commands and PHP5 uses it by default. http://www.php.net/overload (I wouldn't be too afraid of the experimental warning, overloading in PHP5 is pretty much the same as in PHP4, and looks like it's here to stay). -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Includes vs. Functions
On Thu, 18 Jul 2002 08:40:08 -0600, you wrote: See below... >At 09:20 PM 7/17/02 -0500, Michael Sims wrote: > >>$superglobals = array("var1", "var2", "var3", "var4", "var5", "..."); >> >>Now inside the function you can do this: >> >>function somefunction ($somevar) { >> >> global $superglobals; >> foreach($superglobals as $varname) { >> global $$varname; //resolves to $var1, $var2, $var3, etc. >> } >> >> //Other stuff here >> >>} > >You're working too hard! > >function somefunction( $somevar ) { > >global $suberglobals; > >extract( $superglobals ); > >//Other stuff here > >} Sorry, but your version is not functionally equivalent to mine. The call to extract() is creating locally scoped versions of those variables which do NOT reference the global versions. For proof, go here: http://mhs-dev.crye-leike.com/test.php Thanks for the pointer to extract() though, I had never used it before. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Includes vs. Functions
At 09:20 PM 7/17/02 -0500, Michael Sims wrote: >$superglobals = array("var1", "var2", "var3", "var4", "var5", "..."); > >Now inside the function you can do this: > >function somefunction ($somevar) { > > global $superglobals; > foreach($superglobals as $varname) { > global $$varname; //resolves to $var1, $var2, $var3, etc. > } > > //Other stuff here > >} You're working too hard! function somefunction( $somevar ) { global $suberglobals; extract( $superglobals ); //Other stuff here } Rick -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Re: Includes vs. Functions
[snip] Chris, thanks for describing your method. The reason I really dislike Functions in PHP is because you have to pass every variable needed by a function, even if that variable is global in the main script, which is a pain in the ass when a function needs a long string of variables. It makes it easier to forget a variable in the list and can make the code look messy. So, that's why I prefer includes, because the code is operating on the same level as the main script and can easily use variables set locally without making them global. I'll use a function if it only needs one or two variables passed to it, but, I find myself using more Includes than Functions because of the variable passing necessary. [/snip] Includes vs. Functions? How about includes that have functions, which a lot of developers do. For instance, I have a function set up for database connections. I use it by including it with pages that require database connections. I think this probably should be Functions vs. Blocks of Code (that might be placed in an included file). Actually I see this as being potentially messier, IMHO. I will have to remember many variable names that are used in the included file, rather than passing variables I am using locally to functions with clearly thought out names. Example; a. I need a variable to hold this standard deviation result. Since I do the stdv calculation in the block I included I need to go see what the name of the variable is and use that, so I don't muck up the code. b. calcStdDev($myCurrentVariable); oh, I need one for here too so, calcStdDev($anotherVariable); I just think that you are selling yourself short, and ultimately your code will be harder to document and maintain. Using blocks is OK, if that block can be 'functionalized' I say do it. For efficiency in processing it really will not make much difference in an included file whether that file is blocks, or functions. Jay "Youre just jealous because the voices are talking to me" * * Want to meet other PHP developers * * in your area? Check out: * * http://php.meetup.com/* * No developer is an island ... * * -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Re: Includes vs. Functions
> >Chris, thanks for describing your method. The reason I really dislike > >Functions in PHP is because you have to pass every variable needed by a > >function, even if that variable is global in the main script, which is a > >pain in the ass when a function needs a long string of variables. It makes > >it easier to forget a variable in the list and can make the code look messy. [snip] > The whole idea behind modularization is that you are creating a "black > box" so to speak, a chunk of code that performs one function, and one > function only. It should present a *minimal* interface to the outside > world. This means that if I have a black box that say, gives me the > current local time in a particular timezone, I should be able to feed > it the name of the timezone, and it should give me back the current > time. I should NOT have to know how it works inside, or care. I feed > it one value, and it returns another value. [snip] To add to this. If you're passing too many values to a function, then maybe that function isn't broken down enough. I remember hearing/reading that if you need to pass more than, I think, 5 or 6 values to a function, then you're passing too many. Martin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Includes vs. Functions
On Wed, 17 Jul 2002 17:46:25 -0400, you wrote: >Chris, thanks for describing your method. The reason I really dislike >Functions in PHP is because you have to pass every variable needed by a >function, even if that variable is global in the main script, which is a >pain in the ass when a function needs a long string of variables. It makes >it easier to forget a variable in the list and can make the code look messy. > >So, that's why I prefer includes, because the code is operating on the same >level as the main script and can easily use variables set locally without >making them global. > >I'll use a function if it only needs one or two variables passed to it, but, >I find myself using more Includes than Functions because of the variable >passing necessary. Maybe I'm misunderstanding what you guys are talking about here, but it seems to me that you believe that including a block of code and calling it as a function are just two different approaches to the same thing, and nothing more. It's true that they can be very similar, but I think you are overlooking one of the major benefits of functions. You speak of passing variables and making them global as if it were simply a necessary evil. Have you stopped to consider that there are times that variable scope works in your favor? There are times when you do NOT want variable $a inside a function to be the same as variable $a outside of it. Functions (and classes) are all about code modularization. Let us say that you have a particular set of code that you know you are going to need to execute several times. So you decide to put it in an external file and include() that code whenever you need it. In this case, all of the variables you use inside that code are global. But, this is not a problem because you are aware of which variables are in use, so there are no conflicts. But let us say that later on you, or some other programmer entirely, want to use your code again in a completely different program. Now, because all of your variables are global, you (or they) have to stop and think"now lets see, this code references $var_a and $var_b...am I using those variables already for other purposes?" If the new program is already using those variables, for some other purpose altogether, then suddenly your included code has clobbered those variables, and now you have bugs. You have introduced a namespace conflict, and this is an example of poorly seperated functionality. The whole idea behind modularization is that you are creating a "black box" so to speak, a chunk of code that performs one function, and one function only. It should present a *minimal* interface to the outside world. This means that if I have a black box that say, gives me the current local time in a particular timezone, I should be able to feed it the name of the timezone, and it should give me back the current time. I should NOT have to know how it works inside, or care. I feed it one value, and it returns another value. Now, if I have implemented this code as a simple block that is include()'ed, now I suddenly have to concern myself with how it works...i.e. I have to know, if nothing else, what variable names it is expecting, and I have to make sure that I am not clobbering any of those variables inside my main script. As far as I know, every non-trivial programming language (or scripting language) has the concept of variable scope. It behaves differently, but the concept is still there. For example, in Perl, a variable that is referenced in a function (or subroutine) is global by default, unless you use the "my" keyword, which makes it only visible inside the subroutine. PHP is backwards from this...it defaults to assuming you want a local copy of the variable, and only makes it global if you tell it to. But imagine if there was no such concept as variable scope, and all variables were global. We'd end up with variables names like $a3t463tew34 just to make sure we're not conflicting with another named variable... If you're saying that you have a group of variables, let us say 10 or 15 or so, that you want to be global EVERYWHERE, then there are easy ways to accomplish that, and still retain the clean seperation of functionality that a function or a class would give you. I would put the names of the variables inside an array and then globalize them by iterating through the array. Example: $superglobals = array("var1", "var2", "var3", "var4", "var5", "..."); Now inside the function you can do this: function somefunction ($somevar) { global $superglobals; foreach($superglobals as $varname) { global $$varname; //resolves to $var1, $var2, $var3, etc. } //Other stuff here } If I have completely misunderstood the thread here, please forgive my rambling post. :-) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Includes vs. Functions
Chris, thanks for describing your method. The reason I really dislike Functions in PHP is because you have to pass every variable needed by a function, even if that variable is global in the main script, which is a pain in the ass when a function needs a long string of variables. It makes it easier to forget a variable in the list and can make the code look messy. So, that's why I prefer includes, because the code is operating on the same level as the main script and can easily use variables set locally without making them global. I'll use a function if it only needs one or two variables passed to it, but, I find myself using more Includes than Functions because of the variable passing necessary. Tom > From: [EMAIL PROTECTED] (Chris Crane) > Organization: Inxdesign.com > Reply-To: "Chris Crane" <[EMAIL PROTECTED]> > Newsgroups: php.general > Date: Wed, 17 Jul 2002 17:05:59 -0400 > To: [EMAIL PROTECTED] > Subject: Re: Includes vs. Functions > > I am not sure about efficiency, myself and I often ask this without much if > anything at all for a response. However, I am the opposite of you, in that I > prefer to create a library file containing all the functions. To make > developing the page in something like Dreamweaver or Frontpage easier, I > global all the variables of a given function, like say Function > StockQuote($sym) { }. They I include the library file in my PHP > webpage.Something like this... > > > > ~blah > > > > > various html > ~blah > ~blah > > > ~blah > ~blah > > > > In the library file, I global the variables; > > function StockQuote($sym) { > > global $Stock_LastPrice, $Stock_Position; > get the data... > do something with the data... > $Stock_LastPrice = This minus that blah blah; > } > > I can use this variable anywhere I would like and it is very WYSIWYG > friendly. > > I am not sure how efficient it is though as I mentioned above. > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Includes vs. Functions
I am not sure about efficiency, myself and I often ask this without much if anything at all for a response. However, I am the opposite of you, in that I prefer to create a library file containing all the functions. To make developing the page in something like Dreamweaver or Frontpage easier, I global all the variables of a given function, like say Function StockQuote($sym) { }. They I include the library file in my PHP webpage.Something like this... ~blah various html ~blah ~blah ~blah ~blah In the library file, I global the variables; function StockQuote($sym) { global $Stock_LastPrice, $Stock_Position; get the data... do something with the data... $Stock_LastPrice = This minus that blah blah; } I can use this variable anywhere I would like and it is very WYSIWYG friendly. I am not sure how efficient it is though as I mentioned above. "Monty" <[EMAIL PROTECTED]> wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... > I generally prefer to include various blocks of code in templates using > include_once() rather than load a functions library and make calls to those > functions within the script. > > Is there a big difference in efficiency and speed of includes vs. custom > functions? I like includes because it's easier to drop blocks of code in a > page design without disrupting the design of the page very much. > > Thanks, > > Monty > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: includes question
>what is the path of the "includes" directory >in a windows dist of PHP (current version)? > >is that where all the dlls are? No. The DLLs are in your "extensions_path" (or something like that). Your include_path is where *YOUR* PHP files can be placed so you can use them in multiple scripts. The simplest example: Imagine you have a masthead and navigation bar that is the *SAME* on every damn page on your site. Now, you could type it a zillion times, or you could rely on your fancy HTML editor to archive it and re-use it in every page for you, but it's *really* easy to create a 'navbar.inc' file, and then in every page do: If you need to change your navigation bar, you just change navbar.inc Now, when PHP goes *LOOKING* for 'navbar.inc', because you did ... The include_path is *where* PHP will look. The first thing I do on any new site is to change my include_path to something like this: include_path = "./:/www/example.com/includes/" This assumes your ISP uses /www/example.com/ as your "home" directory -- the one with 'htdocs' (or 'www' or 'web' or wherever you upload your HTML) You can then throw all your include files in the 'includes' directory. This is because you do *NOT* want malicious users surfing *directly* to: http://example.com/navbar.inc There are just too many ways that could be abused to do nasty things and break into your site, once you start putting database passwords and other cool stuff in your include files. Meanwhile, back to the include_path "./:/www/example.com/includes/" The "./" part says to look in the current directory first. The ":" part separates one directory from the next. (Use ; in Windows.) You can list as many directories as you want. *Occasionally*, in a complex site, I'll have a different include file with the same name in the same directory as my HTML, and it will "over-ride" the other include file in the includes directory. That's why I like to have "./" first. A truly paranoid site with multiple developers should probably *NOT* do that, since it would be too easy for a file to end up in the wrong place... But you can make the include_path list whatever makes sense to you. I also use include for: 1. CSS, when I'm forced to use it 2. META tags of description/content 3. 'globals' functions that I use in every page -- most notably a font() function that avoids the tedium of typing long FONT tags, without using the badly-broken CSS (see 1.) 4. Any large block of display that needs to be the same in two pages due to goofy design by the client. 5. Database connections, to get them out of the public directory, and, more importantly, for when I have to move to a different web-server or change the password or whatever there's only one (1) place it's written down. -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Includes
we still use include(); setup.php has something like: if(file_exists($page) and ...more...controls..) include_once $page; then the $page.php is where you have a part of your site. You can pass $page as the GET variable: setup.php?page=page.php and so on Maxim Maletsky Founder, Chief Developer PHPBeginner.com (Where PHP Begins) www.PHPBeginner.com [EMAIL PROTECTED] Sebastian A. writes: > Lately I have noticed many scripts that all consist of one file even though > they create the appearance of many pages. For example, you would open > setup.php and a form would appear. Then after you complete the form a new > page appears with the results of your form, however the URL is still > setup.php. So basically you can make complicated forms span only one file > instead of having separate file to gather, display and save the data, how is > this done? > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > Maxim Maletsky Founder, Chief Developer PHPBeginner.com (Where PHP Begins) www.PHPBeginner.com [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php