Solved

What is "protected static?"

Posted on 2016-07-18
7
52 Views
Last Modified: 2016-07-20
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 :)...
0
Comment
Question by:brucegust
  • 4
  • 2
7 Comments
 
LVL 43

Assisted Solution

by:zephyr_hex (Megan)
zephyr_hex (Megan) earned 350 total points
ID: 41717466
Static properties are not associated with a particular instance.  In other words, all StatementImages will have $debug = false, while StatementImage A could have a different $statementid than StatementImage B.
0
 
LVL 43

Expert Comment

by:zephyr_hex (Megan)
ID: 41717467
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 ?
0
 

Author Comment

by:brucegust
ID: 41717508
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!
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 43

Assisted Solution

by:zephyr_hex (Megan)
zephyr_hex (Megan) earned 350 total points
ID: 41717525
Correct.  Sort of.  You could have a dynamic / non-static property that's assigned a value in the constructor.  So you would have the same value for the property across all instances of the class when they are first instantiated, and you wouldn't have to "assign" the value to the property because it's handled in the constructor.

Perhaps this will help clarify the difference.  Consider the implications of the following aspect of static properties:  there is only one copy of a static property and it's shared among all instances of the class.  You can change it... but it changes for all instances.
0
 

Author Comment

by:brucegust
ID: 41717545
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?
0
 
LVL 43

Accepted Solution

by:
zephyr_hex (Megan) earned 350 total points
ID: 41717653
If that approached were used, each instance of StatementImage would have its "own" debug property.  That means if you instantiated a new instance, debug would be false, you could change it to true in that instance, but all of the other StatementImage objects would have debug = false.

But yes, you can set the value in the constructor like that, and then that property is assigned a value when the instance is instantiated.
0
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 150 total points
ID: 41717945
When I think of static properties and methods, I think that "static" is an overloaded term.
http://php.net/manual/en/language.oop5.static.php

In addition to late static bindings, you can get access to static properties and methods without creating an object instance from the class.  Thus the class method can act as a "helper function" for other programming.  In the case of a protected static method. the class method can act as a helper function for this class and extensions of class, but it cannot refer to $this, nor can it be a helper function for programming that is outside the inheritance chain.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Wordpress - Adding an if/else Conditional Argument in a Post Template 4 37
Unwanted output from my query 5 40
while read ID; do 4 54
Find RGB colors from a screen. 2 16
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question