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 8130005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:31:50+00:00 2026-06-06T08:31:50+00:00

I am going through a list of objects, all with the same parent class

  • 0

I am going through a list of objects, all with the same parent class but they could be instances from one of many sub classes. Depending on the true type of the object I want to perform slightly different operations.

How can I run different blocks of code based on the object types?

In Java I would overload a processing function with each type I wanted to work with, but PHP doesn’t allow this.

Background:
To Gordon’s comment.

We are building forms from a group of objects. A super class Component that is extended to represent specific types of form elements. TextInput extends Component and the Component has the ability to hold other Components. We don’t want them to be responsible for their actual representation.

We are working to build HTML right now, but we might potentially want to use them to build .PDFs or other formats.

So we have code that goes through the Components, and recursively through the Components it contains. Depending on the type of the Component, we need to create different output.

  • 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-06-06T08:31:52+00:00Added an answer on June 6, 2026 at 8:31 am

    Use

    if($myobject instanceof class1){
    }elseif($myobject instanceof class1){
    }
    

    Or

    switch(get_class($myobject)){
        case 'class1':
           break;
        case 'class2':
           break;
    }
    

    Also, make sur you don’t break the Open/Closed principle. If you put that portion of code in a class and not in a specific portion of a controller, you end up breaking an important reusability feature of your code:

    Open / Closed Principle

    Have you never done a plug-in system? If you did, i’m sure you didn’t
    ask your users to hardcode a loading line in the code to handle that
    plug-in right?

    Normaly speaking, you’d want your classes to be extendable without
    needing to change the parent. Extension should be an open principle
    where adding more code should be as easy as extending but you should
    never have to modify the parent class.

    Thus addind a switch inside the parent or inside another class to handle
    all possible versions of that class breaks the open principle, because users
    then have to change the class to add more features…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting a many-to-many list in one of my objects. I have three
I was going through the list of predefined Exceptions in PHP and I noticed
In Ruby, I'm used to using Enumerable#inject for going through a list or other
I was going through a book studying Linked list and saw these lines if(
Going through the microsoft authentication tutorial listed here they have you create a master
I've got a function that checks a list of objects to see if they've
I'm trying to access the previously iterated element in a loop going through all
Ok here i am going to list objects of my software. Currently memory usage
is there any way of going through all the new/modified entities and setting their,
I'm going through the process of learning backbone and don't understand all of the

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.