Jump to content

Archived

This topic is now archived and is closed to further replies.

scottybwoy

Quick Classes Question *SOLVED*

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Depends on your classes constructor.

(below is PHP5)
[code]<?php

class Foo
{
    protected static $bar;

    public function __construct ($bar)
    {
        self::$bar = $bar;
    }
}
?>[/code]

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

[code]<?php

class Foo
{
    protected $bar;

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

?>[/code]

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

Share this post


Link to post
Share on other sites
So in the bottom one, $bar could change throughout the execution of the script however the static one will remain the same?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

×

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.