global object from object?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • WhyteWolf

    global object from object?

    I'm trying to set a object as global for access through out the rest
    of my script ... {a basic SQL accessing object} however if I try
    calling the object from with in another object it acts as if it isn't
    global and hasn't been accessed at all ... weather I decalre it from
    inside the object or declare it outside the object
    I would rather not have to resort to the standard of refrenceing sence
    I plan on having modular support and having this one object be a
    global rathen then everyone refrencing it would just be easyer for
    people to understandanywa ys heres some of the code
    and mind you this is pretty basic so far


    ---------index.php----------------
    <?php
    include_once("c onfig.php");
    include_once("l ibs/mod.class.php") ;
    include_once("l ibs/DataBase.class. php");
    include_once("l ibs/main.class.php" );
    $site= new main();
    $site->init();
    $site->loop();
    $site->close();
    ?>
    --------end------------------------
    --------main.class.php-----------
    <?php
    global $db;
    class main {

    function init () {
    global $db;
    $db = new DBAccess($confi g['database']);
    }
    function loop () {}
    function close () {
    $db->DBKill();
    }
    }
    ?>
    ----end-------------------------

    ---error------------------------
    Fatal error: Call to a member function on a non-object in
    main.class.php on line 11
    -------------end----------------


    I would rather get this finished before continuing to work on the code
    sence accessing the database will lead to 90% of the work after this

    and I have googled for the answer already and come up rather dry
  • Garp

    #2
    Re: global object from object?


    "WhyteWolf" <whytewolf@fo ur-twenty.com> wrote in message
    news:tc96e01eob pkghn7kclcglfe0 q78gpkgu2@4ax.c om...[color=blue]
    > I'm trying to set a object as global for access through out the rest
    > of my script ... {a basic SQL accessing object} however if I try
    > calling the object from with in another object it acts as if it isn't
    > global and hasn't been accessed at all ... weather I decalre it from
    > inside the object or declare it outside the object
    > I would rather not have to resort to the standard of refrenceing sence
    > I plan on having modular support and having this one object be a
    > global rathen then everyone refrencing it would just be easyer for
    > people to understandanywa ys heres some of the code
    > and mind you this is pretty basic so far
    >
    >
    > ---------index.php----------------
    > <?php
    > include_once("c onfig.php");
    > include_once("l ibs/mod.class.php") ;
    > include_once("l ibs/DataBase.class. php");
    > include_once("l ibs/main.class.php" );
    > $site= new main();
    > $site->init();
    > $site->loop();
    > $site->close();
    > ?>
    > --------end------------------------
    > --------main.class.php-----------
    > <?php
    > global $db;
    > class main {
    >
    > function init () {
    > global $db;
    > $db = new DBAccess($confi g['database']);
    > }
    > function loop () {}
    > function close () {
    > $db->DBKill();
    > }
    > }
    > ?>
    > ----end-------------------------
    >
    > ---error------------------------
    > Fatal error: Call to a member function on a non-object in
    > main.class.php on line 11
    > -------------end----------------
    >
    >
    > I would rather get this finished before continuing to work on the code
    > sence accessing the database will lead to 90% of the work after this
    >
    > and I have googled for the answer already and come up rather dry[/color]

    Variables in PHP are considered local unless declared 'global' in a given
    scope; your initial 'global $db' is not required and indeed makes no sense
    at file scope.

    Example from the variable scope help (at
    PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.

    <?php
    $a = 1;
    $b = 2;

    function Sum()
    {
    global $a, $b;

    $b = $a + $b;
    }

    Sum();
    echo $b;
    ?>


    Garp


    Comment

    • WhyteWolf

      #3
      Re: global object from object?

      On Wed, 30 Jun 2004 22:58:53 GMT, "Garp" <garp7@no7.blue yonder.co.uk>
      wrote:
      [color=blue]
      >
      >"WhyteWolf" <whytewolf@fo ur-twenty.com> wrote in message
      >news:tc96e01eo bpkghn7kclcglfe 0q78gpkgu2@4ax. com...[color=green]
      >> I'm trying to set a object as global for access through out the rest
      >> of my script ... {a basic SQL accessing object} however if I try
      >> calling the object from with in another object it acts as if it isn't
      >> global and hasn't been accessed at all ... weather I decalre it from
      >> inside the object or declare it outside the object
      >> I would rather not have to resort to the standard of refrenceing sence
      >> I plan on having modular support and having this one object be a
      >> global rathen then everyone refrencing it would just be easyer for
      >> people to understandanywa ys heres some of the code
      >> and mind you this is pretty basic so far
      >>
      >>
      >> ---------index.php----------------
      >> <?php
      >> include_once("c onfig.php");
      >> include_once("l ibs/mod.class.php") ;
      >> include_once("l ibs/DataBase.class. php");
      >> include_once("l ibs/main.class.php" );
      >> $site= new main();
      >> $site->init();
      >> $site->loop();
      >> $site->close();
      >> ?>
      >> --------end------------------------
      >> --------main.class.php-----------
      >> <?php
      >> global $db;
      >> class main {
      >>
      >> function init () {
      >> global $db;
      >> $db = new DBAccess($confi g['database']);
      >> }
      >> function loop () {}
      >> function close () {
      >> $db->DBKill();
      >> }
      >> }
      >> ?>
      >> ----end-------------------------
      >>
      >> ---error------------------------
      >> Fatal error: Call to a member function on a non-object in
      >> main.class.php on line 11
      >> -------------end----------------
      >>
      >>
      >> I would rather get this finished before continuing to work on the code
      >> sence accessing the database will lead to 90% of the work after this
      >>
      >> and I have googled for the answer already and come up rather dry[/color]
      >
      >Variables in PHP are considered local unless declared 'global' in a given
      >scope; your initial 'global $db' is not required and indeed makes no sense
      >at file scope.
      >
      >Example from the variable scope help (at
      >http://uk.php.net/manual/en/language...es.scope.php):
      > <?php
      > $a = 1;
      > $b = 2;
      >
      > function Sum()
      > {
      > global $a, $b;
      >
      > $b = $a + $b;
      > }
      >
      > Sum();
      > echo $b;
      > ?>[/color]
      I understand that unforchantly it doens't help with the problem ...
      the first global $db was an atifact I forgot to remove from messing
      around with this ... the fact remains that I can't call the global $db
      from inside an object weather or not the global was created in the
      object or not, basicly I can't access varables or objects that are
      created outside the object
      basicly from what I'm seeing in the testing that I'm doing is the the
      refrence is local even with "global" when dealing with objects and
      that I'm most likely going to have to use the refrencing anyway

      Comment

      Working...