Jump to content

Archived

This topic is now archived and is closed to further replies.

Ninjakreborn

moving up a step(define, user functions, master config page)

Recommended Posts

It is time, I have seen it enough, and wanted to do it, and now I think I am at that level.  One level after this, I will be classes/object(oop), but for now, I recently got heavy into files and learnt how to handle files better.  There are 2 more things I need to learn to do, before I am happy, this phase, and oop.
For now it's this phase. 
Now, I have decided(I have seen a lot of friends do this.

I am not wanting to create an entire custom templating system yet, I am just wanting to build a function manager, I have seen a lot of people doing it, but I have a really good dea on how to make it work, but I have some questions.

1. I see people generally call them config files, I guess that is what I am going to call it, what do you call yours.

2. Define, I already know what it does but on a basic level, it just defines a value, as permanent.  Is there a point, what is it used for, when I see people's system files, or config file, a lot of times they define  a bunch of stuff, what does it do, and is it safe.

3. Serialize, and unserialize, I understand then b ut they make no sense.  I don't get the point in ever having to serialize or unserialize, can someone show me an example.

4. Ok, I have seen a lot of people do this, and htis is the mail reason I want to set this up.  I want to make a config file, but I think people create a basic copy, and end up replicating it.
So I would just create everything I need, then when I need to use it for a project, I just copy all my files over to where I am working, and that's it.

5. I want to make a huge page, with includes to different pages with functions, but then have the names saved as variables, I don't know what it's called, I have seen people have a php page, with a bunch of includes(comments above each include), then on other pages, all those includes are disabled.
Ok like this, they have there config page, I see at the top they have there mysql connection, session_start, and some other stuff.  Then below that they end up having whatever they have, like error reporting or something, then below that, they have a huge list of includes, to different pages for functions.  Like an include called files, which has all the functions they use pertaining to files, then some people even have the files include page, listed with other includes splitting up files into various sections.  or something.  I don't know how I am going to do it, but when they finally get to the page where they include the config file, all the includes start off disabled, and htey have to put a line of code, for each function page they want to be included, how do I do that, is that have to do with define or something.

6. I have seen in php.ini where it set's foo to an empty string, does this mean in situations where I have php.ini access, that I can create variables within the php.ini to have access to them anywhere, is this safe if it does work like that.

7. I noticed people could get into my includes folder, so I restricted it with htaccess, but then it didn't even include them on the pages, how can I prevent people from getting into it, when I also restrict my css files, I was hoping to cut them off, so people couldn't upload them in some situations, but it cut's it off from the browser as well.

Thanks for the help.

Share this post


Link to post
Share on other sites
1. This depends. If your talking about a file where all my application wide constants are declared, yes I call it a config file. If its a file containing functions, I call it a lib file. This is all personal preference though.

2. I prefer to use constants for most application wide values. They are safer because they cannot accidently be overiden.

3. You understand them but they make no sense? That makes no sense. You will find them more usefull when you get into OOP. Very usefull for saving an objects state.

4. The way my cms works. I use the same framework, but create a new config file for each site.

5. Im not sure what your talking about here really. I have one file (tsp.framework.php) which is included into all my pages. This one file defines my include path using [url=http://php.net/set_include_path]set_include_path[/url]. My function are all included within this page, then all my classes are automatically included using [url=http://php.net/manual/en/language.oop5.autoload.php]autoload[/url]. A typical page might start like....

[code=php:0]
<?php
  include "tsp.framework.php";
  include "utils.php"
  tsp_init();
  // code in here.
  tsp_kill();
?>
[/code]

tsp_init() and tsp_kill() are two functions responsible for setting up logging, error handling, debugging and buffering if needed.

6. That is not what the php.ini is for.

7. The very first line of code in my application is...

[code=php:0]
define("INTSP",TRUE);
[/code]

Then... the first line in any included file is....

[code=php:0]
if (!INTSP) {
  exit();
}
[/code]

This prevents any code within the include being called directly. You should never have includes output anything though any way.

Share this post


Link to post
Share on other sites
well technically, about the includes not outputting anything, do you mean for navigation, I always use includes for header/footer/nave, other elements that are widely changes, or did you mean something else.

Share this post


Link to post
Share on other sites
My includes contain functions. They output nothing unless the functions are called.

Share this post


Link to post
Share on other sites
A well designed application will have only one point of input, and only one point of output.

What does this mean in the real world? The input we can leave for now, that's something more to look at when you get into OOD/P, but with regards to output - this means all output will occur in one place. The ideal being you will have only one 'echo' or 'print' (etc.) anywhere in your application.

However due to performance and readbility this is not always the actual ideal.

So, for an example, all functions should return values, rather than output directly and so forth.

Share this post


Link to post
Share on other sites
1. for me, a config file holds my definitions and variables used throughout (such as database connection variables). a lib file is usually a class of some sort, and i also usually have a [i]inc.functions.php[/i] that is just a basic include file with generalized functions.

2. define is great for any constants on your page. when you get into OOP, you'll also like using define when you have variables you want to be able to access in a class without having to refer to them globally. think of it along the lines of a const variable in other languages

3. i love to use serialize and unserialize as thorpe suggests: to save states. it's also very useful when you're having to pass a ton of information from one page to the next. you can serialize it and pass one variable as opposed to passing each one individually

4. as thorpe has mentioned, a config file basically contains the variables and definitions required to make a script work in a given environment. for instance, when i have my MySQL backup application in place, i can install it on any server for any database by changing the variables and definitions within my config file alone. it allows for flexibility

5. i use a method like this sometimes, and i just have an [i]inc.startup.php[/i] file. this file then includes all of my lib and include files. that way, from any given location, i simply have to include this one file, and i'm good to go for the page. really helps to simplify and application framework

6. i don't think i've ever seen what you're talking about

7. thorpe has about the best suggestion: you want to create a link from your main pages to those being included. if the link is not present, don't let them see it.

Share this post


Link to post
Share on other sites
[code]A well designed application will have only one point of input, and only one point of output.

What does this mean in the real world? The input we can leave for now, that's something more to look at when you get into OOD/P, but with regards to output - this means all output will occur in one place. The ideal being you will have only one 'echo' or 'print' (etc.) anywhere in your application.

However due to performance and readbility this is not always the actual ideal.

So, for an example, all functions should return values, rather than output directly and so forth.[/code]
Ok can you go over this with me a few times, I am far from familiar with this, I Don't understand.
Everything I was taught, I go in, I do a layout, cut out header/footer/nav, and those are my includes, if I put them in a function, I have to mess with my php code, to get it to work properly
this theory one point of input, one point of output, what is this part about, I never heard that before.

As far as all the other advice, the config file makes more sense to me I am going to start constructing one, not too far from now, thanks.

Share this post


Link to post
Share on other sites
This is something your not simply going to [i]get[/i] overnight. Especially IMO if you haven't dealt with OOP yet. Try to think about what happens when someone requests a page from your application. There should be a series of events that your application will always follow. Sure it may branch off into different proccessses in the middle tear but, the initial entry point kicks it all off, and the whole process finishes by sending the response back to the client.

A search for infomation on the MVC pattern may also help though Im thinking at this point in time it may be a little over your head.

Share this post


Link to post
Share on other sites
Also search for IPO (Input, Processing, Output)

That will explain, in grave detail, what I am speaking of. :)

It's a well established (i.e. old) design structure that basically separates the input, the processing, and the output. Some word argue it was/is the foundation of the MVC pattern.

Share this post


Link to post
Share on other sites
compared to many here, i'm relatively new to much of this stuff in terms of design patterns/MVC/IPO, but have managed to implement it quite well and the benefits are amazing.

vastly oversimplifying some of my own practices and what i've learned from some frameworks, one point of output generally means building up your HTML in a variable/object/etc and outputting it with a single 'echo' at the end.
all HTML/content that you'd normally throw out with an echo, you add it to this variable instead.

as for the one point of input, this is generally a case of using the URL params to decide which files to include or which objects to use. you'll probably have seen something like:

[code]
<?php
$module = $_GET['module'];

switch ($module)
{
  case 'home':
      include('inc/home.php';
      break;
  case 'signup':
      include('inc/signup.php');
      break;
}
?>
[/code]
the above example, put into a single index.php file, could feed the user the homepage or the signup page just with a different URL.

The best part about single points of entry/exit (for me, anyway) is just the level of control and scalability it gives me. as all requests come through one page, i have one central place to deal with making sure the $_POST and $_GET arrays are 'clean', and one place to remember when it comes to putting links in my pages - in the event of, for example, me restructuring the entire site and moving things around.

Of course, like I say, this is vastly over simplifying it, but on the other hand, it doesnt really need to be that complex. I actually learnt a hell of alot from the Code Igniter framework (www.codeigniter.com) and its online manual, as it gave me some excellent ideas for structuring things as well as a bit of an OOP crash course, as it's one of the easier ones to get your head around.

Cheers
Mark

Share this post


Link to post
Share on other sites
yes, I am going to start building a basic framework, for regular website creation I think.
There are things I do, that I could put in a function, and then it would cut down about 30 minutes of programming time, instead just have one huge function

Share this post


Link to post
Share on other sites
[quote]instead just have one huge function[/quote]

Im not certain what you mean by this comment, however... you should try and make your functions specific to one task and one task only.

Share this post


Link to post
Share on other sites
[code]Of course, like I say, this is vastly over simplifying it, but on the other hand, it doesnt really need to be that complex. I actually learnt a hell of alot from the Code Igniter framework (www.codeigniter.com) and its online manual, as it gave me some excellent ideas for structuring things as well as a bit of an OOP crash course, as it's one of the easier ones to get your head around.[/code]

I didn't see that there before, I will download that right now, and start playing with it.
I built my basic framework but maybe looking around through that will give me some idea's of what I can add to it, thanks.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.