Jump to content


Photo

Object oriented php programming


  • Please log in to reply
7 replies to this topic

#1 bitman

bitman
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 16 June 2006 - 01:53 PM

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.

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 16 June 2006 - 02:30 PM

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.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 bitman

bitman
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 16 June 2006 - 05:00 PM

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???

#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 16 June 2006 - 05:08 PM

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) View Post[/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, but i'm sure some of the other mods/admins will have more to add as well:
[/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\" /]
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 16 June 2006 - 06:36 PM

[!--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) View Post[/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.

#6 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 16 June 2006 - 11:51 PM

[!--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).
<?php

  class foo {

    private static $instance;

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

}

?>
Now, to use foo you simply do...
<?php

  $foo = foo::getinstance();

?>


#7 bitman

bitman
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 17 June 2006 - 09:42 AM

Thanks thorpe.
thats what i was looking for.

#8 bitman

bitman
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 17 June 2006 - 11:04 AM

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:
<?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;
    }
}

?>

and i have two pages:
index.php:
<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>

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

$single = singleton::GetInstance();

echo $single->gettest();

?>

<b> this is page 2 </b>

</BODY>
</HTML>

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..




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users