Jump to content

Recommended Posts

Hello all.
i want to develop a php application for my web site.
i have decided to take the object-oriented approach since,
well , since i like it..

my problem is i am kind of new to web programming and missing some vital information.
hope you guys can help me with these:

1. how do i make sure a main class i write will be available in every one of the script pages?
do i need to make it a session variable? and save it to the session every time i make a change
of the data? or maybe i need to include the loading of the data in every one of the
scripts and pages of the application?

2. i guess this is very similar to the first question - how do i open one db-connection
at the loading of the application and use it throughout the different pages of my site?
is there any reason (security or such) not to keep the connection open, and reopen
it with every query?

3. how does php know when a user switches application? by the url? i guess my question is,
if i have a link in my site, to a different site, how does it know not to pass the session
variables to that site when a user clicks the link?

4. if i define a global variable, does it have the same scope as a session or application variable?

i guess i missed the correct documentation since all my questions regard to scope in web application.
i learned php from a php manual that contained full reference, but no guide...
so if anyone will be willing to point me to the right documentation section i will be very
gratefull..
thanks.
Link to comment
https://forums.phpfreaks.com/topic/12154-object-oriented-php-programming/
Share on other sites

bitman, good questions, especially to get a handle on when you're starting. this is the type of things that you need to have a grasp on to create successful web applications. i'll try to answer them as best i can, but i'm sure some of the other mods/admins will have more to add as well:

1) The declaration of the class will have to be included on every page that uses that class. One great solution is simply to have a shared header file that includes all required documents for the whole site, and then simply have every file then include that header.

2) Same method as above. if you open the database connection in a common include file, you only have to work with it in one location, but it will connect every page in which it is included.

3) i'm not sure i entirely understand what you mean by "switching applications". if they are switching to another app on your server, the session will be the same if your domain is the same. unless you have declared otherwise, a session is valid for the current domain. so, if you switch to another domain, you end up with the session not being valid. any other restrictions based on location and such are up to you to determine within the code and logic yourself.

4) defining a variable as global simply allows the current function, class, or other scope you are inside of to recognize those variables that have been declared outside of that scope. for instance: if you declare the variable $conn for your database connection, and a function needs that connection reference, you simply would have to either pass the variable to the function through a parameter, or you would declare that variable as "global" within that function.

i hope this helps you somewhat. web programming is similar in some respects to application programming, but as far as these questions go, you'll notice that there is quite a difference between them.
Hey, thanks obsidian,
your replies sure help.

i still want to clarify something regarding the first question\answer.

If i say what you suggest, that is include all my needs in the shared header files,
i will be able to define my classes in every one of the variable.
but my question was a little different. i want to have a shared 'instance' of that
class. like a static\global general class instance that is shared for all pages.
i think this is called a single tone in application programming..
is there anyway to implement something like that with php?

(this way if my db connection resource is part of that class, i dont need
to connect every time as well).

p.s.
how much posts do you have to post to get to super-guru???
well i was gonna throw in my 2 cents on some of these issues, but
[!--quoteo(post=384568:date=Jun 16 2006, 09:30 AM:name=obsidian)--][div class=\'quotetop\']QUOTE(obsidian @ Jun 16 2006, 09:30 AM) [snapback]384568[/snapback][/div][div class=\'quotemain\'][!--quotec--]
bitman, good questions, especially to get a handle on when you're starting. this is the type of things that you need to have a grasp on to create successful web applications. i'll try to answer them as best i can, [b]but i'm sure some of the other mods/admins will have more to add as well:[/b]
[/quote]
i guess since i'm not a mod/admin i can't. [img src=\"style_emoticons/[#EMO_DIR#]/huh.gif\" style=\"vertical-align:middle\" emoid=\":huh:\" border=\"0\" alt=\"huh.gif\" /]
[!--quoteo(post=384659:date=Jun 16 2006, 06:08 PM:name=Crayon Violent)--][div class=\'quotetop\']QUOTE(Crayon Violent @ Jun 16 2006, 06:08 PM) [snapback]384659[/snapback][/div][div class=\'quotemain\'][!--quotec--]
well i was gonna throw in my 2 cents on some of these issues, but

i guess since i'm not a mod/admin i can't. [img src=\"style_emoticons/[#EMO_DIR#]/huh.gif\" style=\"vertical-align:middle\" emoid=\":huh:\" border=\"0\" alt=\"huh.gif\" /]
[/quote]
huh? You can just reply to this thread! That was what obsidian was saying.
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]i think this is called a single tone in application programming..[/quote]
Its called a singleton pattern, and yes, it can be achieved within php. A simple example (php5).
[code]
<?php

  class foo {

    private static $instance;

    public static function getinstance() {
      if (empty(self::$instance)) {
        self::$instance = new foo();
      
      }
      return self::$instance;
    
    }

}

?>
[/code]
Now, to use foo you simply do...
[code]
<?php

  $foo = foo::getinstance();

?>
[/code]
hey again all you generous helpers,
i tried writing a test to see if this code really acts like i thought,
and well, it doesnt really.

my class is:
[code]
<?php
// file singleton_class.php
class singleton
{
    private static $instance;
    private $test;
    
    public static function GetInstance()
    {
        if (empty(self::$instance))
        {
            self::$instance = new singleton();
            echo 'get instance';
        }
        return self::$instance;
    }
    
    public function settest($value)
    {
        $this->test = $value;
    }
    public function gettest()
    {
        return $this->test;
    }
}

?>
[/code]

and i have two pages:
index.php:
[code]
<HTML>
<BODY>
<?php
include_once('singleton_class.php');

$single = singleton::GetInstance();
$single->settest("lalala");
echo $single->gettest();

?>

<a href="page2.php"> click </a>

</BODY>
</HTML>
[/code]

and page2.php:
[code]
<HTML>
<BODY>
<?php
include_once('singleton_class.php');

$single = singleton::GetInstance();

echo $single->gettest();

?>

<b> this is page 2 </b>

</BODY>
</HTML>
[/code]

in both pages i get the line 'get instance' printed from inside the getinstance() method...
which means that the instance is a new one in every page.
i thought it will be the same instance, otherwise what is the value of making it static?
i suppose i misunderstand something..
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.