Avatar of Bruce Gust
Bruce Gust
Flag for United States of America asked on

What is "protected static?"

If I were to explain what a property was in OOP and then go on to explain a constructor, I would ask my audience to envision a spreadsheet. A property is like a column. It's just a digital placeholder. A constructor assigns a value to that column.

"Visibility" is all about how accessible those properties in the context of your app. Public means that it's available throughout your entire online community. "Protected" means that it's only available within the Class that it was instantiated along with any Classes it might be associated with (parent / child). "Private" means that it's only available within the Class it was created in.

"Protected Static." I get the "protected" part and I've attempted to google "static," but I'm still a little fuzzy.

From what I can gather, making a Property "static" eliminates the need for it to be instantiated using the $this (pseudo variable which means "this current object"). Frankly, I've never used that, but I want to understand it. Here's the code I'm attempting to deconstruct:

class StatementImage {

    protected $statementid;
    protected $pipe_delim;
    protected $statement_data;
    protected static $debug = false;

    /**
     * Takes the pipe delimited file and creates a statementimage entity.
     */
    public function __construct($pipe_delim) {

        $this->pipe_delim = $pipe_delim;
        $this->statement_data = StatementImage::pipeToArray($pipe_delim);
        $this->statementid = str_replace('T', '', $this->statement_data['statement']['statement_pin']);

    }

Open in new window


What is $debug=false? It's not referenced anywhere else on the page and I cannot figure out it's purpose or why it would be assigned a "protected static" dynamic.

Please raise your hand if you know the answer :)...
PHP

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon
SOLUTION
zephyr_hex (Megan)

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
zephyr_hex (Megan)

And you're correct, $debug is not being referred to in the code you posted.  Perhaps it's referenced somewhere else?   Or maybe it was created for debugging but the related code has been removed ?
Bruce Gust

ASKER
I get it...

So, anytime you include a static property within a class, every function within that class will AUTOMATICALLY have that property attached to it.

So, in this instance, instead of having to repeatedly assign a "false" value to the $debug property (although we don't know where that puppy is right now), because it's "static," it's present throughout by default, yes?

Thanks!
SOLUTION
zephyr_hex (Megan)

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Bruce Gust

ASKER
You're right! I get what you're saying about the dynamic, non-static property scenario in the constructor - that would work. So, conceivably there are two different ways in which the guy who originally wrote this code could've done what he was striving to accomplish with the "protected static $debug=false" code.

He could've used:

protected static $debug=false

OR

He could've done something like:

protected $debug

public function __construct {

$this->debug=false;

}

Yeah?
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER CERTIFIED SOLUTION
zephyr_hex (Megan)

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Ray Paseur

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.