Declaring a variable across objects

Hi all, got an OOP related question. I would really appreciate your help. Apologies if my terminology is not the best with this but here I go:

Is it possible to set a variable in a class from the outside that will remain set for every instantiated object?

Thanks again.

Found the answer:

Class::$variable=‘Rock & Roll’;

And I think it has to be declared as a static variable inside the class.

Your options are;

1 Access a global variable.
2 Access a constant.
3 Access a variable via a static method.
4 Pass the variable in with the constructor.

examples of each - but should read as an example of how to do each.

1.


// make sure this is included and available to myclass
$skycolor =  'blue' // this is in the global namespace


// corrected...
class myclass {

function getSky(){
global  $skycolor;
return 'the sky is ' . $skycolor;
}

}

// usage
$a = new myclass;
echo $a->getSky(); // the sky is blue  


2.


// make sure this is included and available to myclass
define('SKYCOLOR', 'blue'); // use all caps for constants


class myclass {

function getSky(){
return 'the sky is ' . SKYCOLOR ;
}

}

// usage
$a = new myclass;
echo $a->getSky(); // the sky is blue

3.


class sky {
static function getColor(){
return 'blue';
}

}

class myclass {

function getSky(){
return 'the sky is ' . sky::getColor();
}

}

// usage
$a = new myclass();
echo $a->getSky(); // the sky is blue

4.


class myclass {
private $color;
function __construct( $color='black' ){
$this->color = $color ;
}

function getSky(){
return 'the sky is ' . $this->color;
}

}

$color = 'blue';

// usage
$a = new myclass($color);
echo $a->getSky(); // the sky is blue

Each method has its own pros and cons, and there are other ways, search for examples of the Singleton pattern or Registry pattern.

This treatise easy to understand design:the_registry [phpPatterns]

Edit:

They are in order of least desirable, accessing a global var being the worst idea, but of course ‘it all depends’, the existing codebase, the size of your application, whether the code is to be distributed and used in other unknown systems and of course personal preference.

I edited some examples to make sure they do work :wink: sorry…

Ha no need to apologize! Thank you so much.
I am sticking with the static approach for my purposes. Good to know all possibilities though.

So is it any better to set the class variable via a method than to declare a static variable in the class and do the following?

Class::$variable='Rock & Roll';

Bescause each config file in a project as part of a cms of mine must conform to certain settings (even if empty) I choose to enforce its method signature with an Interface - that way the warning message is found early and is very specific.


class WSC_Config implements IWSC_Config {

    private static $PROJECT_PATH    = "/var/www/html/website"; 
                                                   // path to public home dir

// accessors for each static member

    final static function getProjectPath(){
    
    return self::$PROJECT_PATH ;

    }
}

Hence, each class member has a predictable accessor method - its probably not necessary but it is “bleeding obvious” what it does whenever I have to read the file - which is half the battle sometimes.

Perhaps not so obvious from this example is the fact that some static methods contain non-scalar values as well:


    final static function getMapLimits(){

    $map_limits['lat']['max'] = 50.208389 ;
    $map_limits['lat']['min'] = 50.172835 ;
    $map_limits['lng']['max'] = -0.685709 ;
    $map_limits['lng']['min'] = -0.653345 ;

    return $map_limits ;

    }

Thanks for the info, appreciated!