Jump to content


Photo

Quick Classes Question *SOLVED*


  • Please log in to reply
7 replies to this topic

#1 scottybwoy

scottybwoy
  • Members
  • PipPipPip
  • Advanced Member
  • 532 posts
  • LocationUK

Posted 01 September 2006 - 09:12 AM

Hi,

Say I have a class called foo.  Foo has a number of functions in it.  Then say I create a new instance of Foo
and pass a parameter to it like so :

$Foofoo = new Foo($bar)

Will the parameter of $bar embed itself into every instance of $bar in the Class so that whenever it is called via other $variables or Functions, it will use $bar when needed?


#2 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 01 September 2006 - 09:20 AM

Depends on your classes constructor.

(below is PHP5)
<?php

class Foo
{
    protected static $bar;

    public function __construct ($bar)
    {
        self::$bar = $bar;
    }
}
?>

That will set the static property of the class to $bar - thus every object that utilises that static property will receive the same value.

<?php

class Foo
{
    protected $bar;

    public function __construct ($bar) 
    {
        $this->bar = $bar;
    }
}

?>

That will set the property of the object to $bar, thus only that instance of the object will maintain the value.

#3 scottybwoy

scottybwoy
  • Members
  • PipPipPip
  • Advanced Member
  • 532 posts
  • LocationUK

Posted 01 September 2006 - 09:57 AM

So in the bottom one, $bar could change throughout the execution of the script however the static one will remain the same?


#4 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 01 September 2006 - 10:19 AM

yup. But if any of your objects change the static property at any stage, it will change for all objects that use it as well.

#5 scottybwoy

scottybwoy
  • Members
  • PipPipPip
  • Advanced Member
  • 532 posts
  • LocationUK

Posted 01 September 2006 - 11:24 AM

That's great, thanks Jenk for clearing that one up.

#6 scottybwoy

scottybwoy
  • Members
  • PipPipPip
  • Advanced Member
  • 532 posts
  • LocationUK

Posted 01 September 2006 - 12:55 PM

One more thing, if I pass a variable to a class like this :

$user_dbi = new DBI($USER_DB_URL);

will user_dbi end up the outcome of $USER_DB_URL being passed through the whole DBI Class?

#7 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 01 September 2006 - 01:05 PM

no, only the constructor.

#8 scottybwoy

scottybwoy
  • Members
  • PipPipPip
  • Advanced Member
  • 532 posts
  • LocationUK

Posted 01 September 2006 - 01:46 PM

OK thanks




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users