RE: [PHP] global class instance

2006-07-07 Thread Richard Lynch
On Wed, July 5, 2006 3:32 pm, KermodeBear wrote:
 i don't want to:
- or use a::print

 $myFoo = Singleton::getFoo();

What's wrong in this picture? :-)

As far as I can tell, the original poster shouldn't be using PHP,
since he wants the language to have some kind of implicit $this

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



Re: [PHP] global class instance

2006-07-05 Thread Eric Butera

On 7/1/06, sempsteen [EMAIL PROTECTED] wrote:

hi all,
i wonder if there is a way of creating an instance of a class and
reach it direcly from any scope in PHP4. basically what i want is:

class a
{
   function print()
   {
  echo 'sth';
   }
}

$a = new a();

and use this a instance from anywhere ex, in a function that is a
method of another class.

class b
{
   function print()
   {
  $a-print();
   }
}

i don't want to:
   - declare global $foo,
   - use pre-defined $GLOBALS variable,
   - or use a::print

thanks.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Static is your friend.

http://www.horde.org/papers/kongress2002-design_patterns/11_singleton_impl.xml.html

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] global class instance

2006-07-05 Thread KermodeBear
 hi all,
 i wonder if there is a way of creating an instance of a class and
 reach it direcly from any scope in PHP4. basically what i want is:

 i don't want to:
- declare global $foo,
- use pre-defined $GLOBALS variable,
- or use a::print

 thanks.

 Static is your friend.


http://www.horde.org/papers/kongress2002-design_patterns/11_singleton_impl.x
ml.html

Also, if using PHP 4, make sure to read the References with global and
static variables section. It doesn't work quite the way a lot of people
expect it to.

http://us2.php.net/static

The manual isn't all that clear, but the user comments help a lot (which, I
have found, is often the case, but that's a rant for another day... At least
we have a manual that accepts such comments!)

class Singleton {
function getFoo() {
$object = Singleton::getFooRef();
if (!is_a('Foo', $object)) {
$object = new Foo();
}
return $object;
}

function getFooRef() {
static $object = null;
return $object;
}
}

$myFoo = Singleton::getFoo();

Hope that helps.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] global class instance

2006-07-03 Thread Richard Lynch
On Sat, July 1, 2006 4:56 am, sempsteen wrote:
 i wonder if there is a way of creating an instance of a class and
 reach it direcly from any scope in PHP4. basically what i want is:

 class a
 {
function print()
{
   echo 'sth';
}
 }

 $a = new a();

 and use this a instance from anywhere ex, in a function that is a
 method of another class.

 class b
 {
function print()
{
   $a-print();
}
 }

 i don't want to:
- declare global $foo,
- use pre-defined $GLOBALS variable,
- or use a::print

No.

Well, okay, maybe if you installed Runkit and declared your own
variable as a SUPERGLOBAL.  Maybe.

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



Re: [PHP] global class instance

2006-07-02 Thread John Wells

On 7/1/06, sempsteen [EMAIL PROTECTED] wrote:

hi all,
i wonder if there is a way of creating an instance of a class and
reach it direcly from any scope in PHP4. basically what i want is:
...
i don't want to:
   - declare global $foo,
   - use pre-defined $GLOBALS variable,
   - or use a::print



Well I don't know what the list will feel about this one, but you
*could* create a function (or series of functions) to do what you want
with $a, since functions always live in the global scope.

If you're just looking for a generic get-me-around-scoping solution,
this obviously wouldn't be appropriate.  However if your $a is
special, for example an application-wide configuration object, then
the approach might suit your needsor wants, as it were.

-John W

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] global class instance

2006-07-02 Thread Chris

sempsteen wrote:

yes i'm calling a lot. actually i have a class that handles mysql
queries named database.
what i want was call a database method from a method of another function.

class

  function
  ...$database-execute_query(...


Always CC the list.

What's wrong with this being a global variable?

In your common or global or whatever you call it file:

$db = new Db();
$db-Connect('');
$GLOBALS['Database'] = $db;

then you use $GLOBALS['Database']-...

This file is *always* called before your other classes so it's always 
set. It can't be overridden or anything, so I don't understand the 
objection to it being a global variable.



Another option, inside your main class (you're using inheritance right?)

var $Db = null;

function GetDb() {
  if (!is_null($this-Db)  is_resource($this-Db-Connection)) {
return $this-Db;
  }
  $db =  new Db();
  $db-Connect(...);
  $this-Db = $db;
}

then use

$db = $this-GetDb();

inside your class.

--
Postgresql  php tutorials
http://www.designmagick.com/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] global class instance

2006-07-01 Thread chris smith

On 7/1/06, sempsteen [EMAIL PROTECTED] wrote:

hi all,
i wonder if there is a way of creating an instance of a class and
reach it direcly from any scope in PHP4. basically what i want is:

class a
{
   function print()
   {
  echo 'sth';
   }
}

$a = new a();

and use this a instance from anywhere ex, in a function that is a
method of another class.

class b
{
   function print()
   {
  $a-print();
   }
}

i don't want to:
   - declare global $foo,
   - use pre-defined $GLOBALS variable,
   - or use a::print


Then you're out of luck.

$a doesn't exist inside class b, how is it supposed to know what $a is ?

--
Postgresql  php tutorials
http://www.designmagick.com/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] global class instance

2006-07-01 Thread chris smith

On 7/1/06, chris smith [EMAIL PROTECTED] wrote:

On 7/1/06, sempsteen [EMAIL PROTECTED] wrote:
 hi all,
 i wonder if there is a way of creating an instance of a class and
 reach it direcly from any scope in PHP4. basically what i want is:

 class a
 {
function print()
{
   echo 'sth';
}
 }

 $a = new a();

 and use this a instance from anywhere ex, in a function that is a
 method of another class.

 class b
 {
function print()
{
   $a-print();
}
 }

 i don't want to:
- declare global $foo,
- use pre-defined $GLOBALS variable,
- or use a::print

Then you're out of luck.


Actually you could pass it in:

function print($a) {
 $a-print();
}

but thats going to cause you lots of pain if you call it a lot (ie you
forget to pass it in everywhere).

--
Postgresql  php tutorials
http://www.designmagick.com/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] global class instance

2006-07-01 Thread Larry Garfield
It sounds like your only other option is a singleton, which works only if 
you're comfortable having only one instance of a, ever.  The following code 
also only works in PHP 5.

class A {
  static obj;

  static instance() {
if (! self::obj instanceof A) {
  self::obj = new A();
}
return self::obj;
  }
}

class B {
  function print() {
$a =  A::instance();
$a-print();
  }
}

(Possibly a syntax error in the above, but hopefully you get the idea.)

On Saturday 01 July 2006 04:56, sempsteen wrote:
 hi all,
 i wonder if there is a way of creating an instance of a class and
 reach it direcly from any scope in PHP4. basically what i want is:

 class a
 {
function print()
{
   echo 'sth';
}
 }

 $a = new a();

 and use this a instance from anywhere ex, in a function that is a
 method of another class.

 class b
 {
function print()
{
   $a-print();
}
 }

 i don't want to:
- declare global $foo,
- use pre-defined $GLOBALS variable,
- or use a::print

 thanks.

-- 
Larry Garfield  AIM: LOLG42
[EMAIL PROTECTED]   ICQ: 6817012

If nature has made any one thing less susceptible than all others of 
exclusive property, it is the action of the thinking power called an idea, 
which an individual may exclusively possess as long as he keeps it to 
himself; but the moment it is divulged, it forces itself into the possession 
of every one, and the receiver cannot dispossess himself of it.  -- Thomas 
Jefferson

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] global class instance

2006-07-01 Thread Larry Garfield
You can simulate singletons in PHP 4 as well.  But you've just eliminated 
every option for accessing a non-local object inside a function.  You've got 
4 options.  Pick one, but I'm fairly certain there is no magical 5th. :-)

On Saturday 01 July 2006 13:02, sempsteen wrote:
 Thanks Larry but i'm working on PHP4. Also you don't use $a directly.
 Instead of writing
 $a =  A::instance();
 i can write:
 global $a

 both takes two steps which is not what i wanted.

 On 7/1/06, Larry Garfield [EMAIL PROTECTED] wrote:
  It sounds like your only other option is a singleton, which works only if
  you're comfortable having only one instance of a, ever.  The following
  code also only works in PHP 5.
 
  class A {
static obj;
 
static instance() {
  if (! self::obj instanceof A) {
self::obj = new A();
  }
  return self::obj;
}
  }
 
  class B {
function print() {
  $a =  A::instance();
  $a-print();
}
  }
 
  (Possibly a syntax error in the above, but hopefully you get the idea.)
 
  On Saturday 01 July 2006 04:56, sempsteen wrote:
   hi all,
   i wonder if there is a way of creating an instance of a class and
   reach it direcly from any scope in PHP4. basically what i want is:
  
   class a
   {
  function print()
  {
 echo 'sth';
  }
   }
  
   $a = new a();
  
   and use this a instance from anywhere ex, in a function that is a
   method of another class.
  
   class b
   {
  function print()
  {
 $a-print();
  }
   }
  
   i don't want to:
  - declare global $foo,
  - use pre-defined $GLOBALS variable,
  - or use a::print
  
   thanks.
 
  --
  Larry Garfield  AIM: LOLG42
  [EMAIL PROTECTED]  ICQ: 6817012
 
  If nature has made any one thing less susceptible than all others of
  exclusive property, it is the action of the thinking power called an
  idea, which an individual may exclusively possess as long as he keeps it
  to himself; but the moment it is divulged, it forces itself into the
  possession of every one, and the receiver cannot dispossess himself of
  it.  -- Thomas Jefferson
 
  --
  PHP General Mailing List (http://www.php.net/)
  To unsubscribe, visit: http://www.php.net/unsub.php

-- 
Larry Garfield  AIM: LOLG42
[EMAIL PROTECTED]   ICQ: 6817012

If nature has made any one thing less susceptible than all others of 
exclusive property, it is the action of the thinking power called an idea, 
which an individual may exclusively possess as long as he keeps it to 
himself; but the moment it is divulged, it forces itself into the possession 
of every one, and the receiver cannot dispossess himself of it.  -- Thomas 
Jefferson

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php