Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 848689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:59:01+00:00 2026-05-15T06:59:01+00:00

I’m working on a framework that I’m trying to type as strongly as I

  • 0

I’m working on a framework that I’m trying to type as strongly as I possibly can. (I’m working within PHP and taking some of the ideas that I like from C# and trying to utilize them within this framework.)

I’m creating a Collection class that is a collection of domain entities/objects. It’s kinda modeled after the List<T> object in .Net.

I’ve run into an obstacle that is preventing me from typing this class. If I have a UserCollection, it should only allow User objects into it. If I have a PostCollection, it should only allow Post objects.

All Collections in this framework need to have certain basic functions, such as add, remove, iterate. I created an interface, but found that I couldn’t do the following:

interface ICollection { public function add($obj) }
class PostCollection implements ICollection { public function add(Post $obj) {} }

This broke it’s compliance with the interface. But I can’t have the interface strongly typed because then all Collections are of the same type. So I attempted the following:

interface ICollection { public function add($obj) }
abstract class Collection implements ICollection { const type = 'null'; }
class PostCollection extends Collection {
    const type = 'Post';
    public function add($obj) {
        if (!($obj instanceof self::type)) {
            throw new UhOhException();
        }
    }
}

When I attempt to run this code, I get syntax error, unexpected T_STRING, expecting T_VARIABLE or '$' on the instanceof statement. A little research into the issue and it looks like the root of the cause is that $obj instanceof self is valid to test against the class. It appears that PHP doesn’t process the entire self::type constant statement in the expression. Adding parentheses around the self::type variable threw an error regarding an unexpected ‘(‘.

An obvious workaround is to not make the type variable a constant. The expression $obj instanceof $this->type works just fine (if $type is declared as a variable, of course).

I’m hoping that there’s a way to avoid that, as I’d like to define the value as a constant to avoid any possible change in the variable later. Any thoughts on how I can achieve this, or have I take PHP to it’s limit in this regard? Is there a way of "escaping" or encapsulating self::this so that PHP won’t die when processing it?

UPDATE

Based on the feedback, I thought of something to try — the code below works!

Can anyone think of

  1. a reason not to do this,
  2. a reason this won’t ultimately work, or
  3. a better way to pull this off?
interface ICollection { public function add($obj) {...} }
abstract class Collection { const type = null; protected $type = self::type; }
class PostCollection extends Collection {
    const type = 'Post';
    public function add($obj) {
        if (!($obj instanceof $this->type)) {
            throw new UhOhException();
        }
    }
}

UPDATE #2:

After putting the code above into production, it turns out it doesn’t work. I have no idea how it worked when I tested it, but it doesn’t work at all. I’m stuck with using a protected variable, I think.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T06:59:01+00:00Added an answer on May 15, 2026 at 6:59 am

    This also works correctly, using a static:

    <?php
    
    interface ICollection { 
      public function add($obj); 
    }
    abstract class Collection implements ICollection { 
      static protected $_type = 'null'; 
    }
    class PostCollection extends Collection {
     static protected $_type = 'Post';
     public function add($obj) {
      if(!($obj instanceof self::$_type)) {
       throw new UhOhException();
      }
     }
    }
    
    
    class Post {}
    
    $coll = new PostCollection();
    $coll->add(new Post());
    

    And actually, you probably want to define your add() method on the Collection class anyway, which means you’ll have to use get_class() to get around some weirdness with self::type or even self::$_type always wanting to return the base Collection class anyway, so this would probably work:

    abstract class Collection implements ICollection { 
      const type = 'null'; 
      public function add($obj) {
       $c = get_class($this);
       $type = $c::type;
       if(!($obj instanceof $type)) {
        throw new UhOhException();
       }
      }
    }
    
    class PostCollection extends Collection {
     const type = 'Post';
    }
    class Post {}
    
    $coll = new PostCollection();
    $coll->add(new Post());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.