DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • Operator Overloading in Java
  • Using Render Log Streams to Log to Papertrail
  • Mastering Go-Templates in Ansible With Jinja2
  • Event-Driven Architecture Using Serverless Technologies

Trending

  • Operator Overloading in Java
  • Using Render Log Streams to Log to Papertrail
  • Mastering Go-Templates in Ansible With Jinja2
  • Event-Driven Architecture Using Serverless Technologies
  1. DZone
  2. Coding
  3. Languages
  4. Accessing protected properties from objects that share the same ancestry.

Accessing protected properties from objects that share the same ancestry.

Evert Pot user avatar by
Evert Pot
·
Sep. 25, 14 · Interview
Like (0)
Save
Tweet
Share
5.19K Views

Join the DZone community and get the full member experience.

Join For Free

I realized something odd about accessing protected properties the other day. It's possible in PHP to access protected properties from other objects, as long as they are from the same class, as illustrated here:

<?php

class MyClass {

    protected $val;

    function __construct($newVal = 'default') {

        $this->val = $newVal;

    }

    function output(MyClass $subject) {

        echo $subject->val, "\n";

    }
}
$obj1 = new MyClass();
$obj2 = new MyClass("hello world");

$obj1->output($obj2);
// Output: hello world

?>

I always thought that protected strictly allows objects to access things from the current inheritence tree, but didn't realize that this also extends to other instances of the same object.

This behavior works for properties and methods, and also when they are defined as private.

The other day, I realized though that this even works for objects of other classes, as long as you are accessing members that are defined in a class that also appears in the accessing class' ancestry.

Sounds a bit complicated, so here's the example:

<?php

class Ancestor {

    protected $val = 'ancestor';

}
class Child1 extends Ancestor {

    function __construct() {

        $this->val = 'child1';

    }

}

class Child2 extends Ancestor {

    function output(Ancestor $subject) {

        echo $subject->val, "\n";

    }

}

$child1 = new Child1();
$child2 = new Child2();

$child2->output($child1);
// Output: child1

?>

Interestingly, if the last example is modified so that the properties are not set in the constructors, but instead by overriding the property, this will break:

<?php

class Ancestor {

    protected $var = 'ancestor';

}
class Child1 extends Ancestor {

    protected $var = 'child1';

}

class Child2 extends Ancestor {

    function output(Ancestor $subject) {

        echo $subject->var, "\n";

    }

}

$child1 = new Child1();
$child2 = new Child2();

$child2->output($child1);
// Output: Fatal error: Cannot access protected property Child1::$var

?>

Because the third example throws an error, but the second does not, this makes me feel that I've simply stumbled upon an edge-case of the PHP engine, and that this feature is not by design. If it were designed as such, both should imho work.

After all if a certain behavior (in this case a property) works for an ancestor, the liskov substitution principle dictates it should also work for any sub-classes.

In addition, PHP normally only allows you to modify a property's visibility to become more visible.

Still, I just ran into a case where the behavior of example #2 is super handy, but I'm not entirely sure if it's a good idea to rely on this behavior, or to assume that this behavior is merely 'undefined' and could be altered without notice in a subsequent PHP version.

The PHP spec does not explictly disallow it though. This is the relevant text:

A member with protected visibility may be accessed only from within its own class and from classes derived from that class. source

Of course the PHP spec is likely not finished yet, and not sure if it's vetted by the PHP team at all.

So I'm left not really knowing wether relying on this behavior is a good idea, but at the very least it's immediately a good example of why having a correct and official PHP standard is a great idea.

Property (programming) Object (computer science) PHP

Published at DZone with permission of Evert Pot, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Operator Overloading in Java
  • Using Render Log Streams to Log to Papertrail
  • Mastering Go-Templates in Ansible With Jinja2
  • Event-Driven Architecture Using Serverless Technologies

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: