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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:51:46+00:00 2026-05-11T03:51:46+00:00

I’d like to accomplish something like this: Call a method, say turn, and then

  • 0

I’d like to accomplish something like this: Call a method, say ‘turn’, and then have ‘turn’ applied differently to different data types, e.g., calling ‘turn’ with a ‘screwdriver’ object/param uses the ‘turnScrewdriver’ method, calling ‘turn’ with a ‘steeringWheel’ object/param uses the ‘turnSteeringWheel’ method, etc. — different things are being done, but they’re both called ‘turn.’

I’d like to implement this so that the calling code needn’t worry about the type(s) involved. In this example, ‘turn’ should suffice to ‘turn’ a ‘screwdriver’, ‘steeringWheel’, or whatever might need to be ‘turned.’

In C++ I’d do this with overloading — and C++ would sort things out based on the datatype/signature — but this doesn’t work in PHP.

Any suggestions as to where should I begin? A switch statement is obvious, but I’m thinking there must be a (more elegant) OO solution. No?

TIA

  • 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. 2026-05-11T03:51:47+00:00Added an answer on May 11, 2026 at 3:51 am

    I read davethegr8’s solution but it seems one could do the same thing with stronger typing:

    <?php  interface Turnable {   public function turn(); }  class Screwdriver implements Turnable {   public function turn() {     print 'to turning sir!\n';   } }  class SteeringWheel implements Turnable {   public function turn() {     print 'to everything, turn, turn turn!\n';   } }  function turn(Turnable $object) {   $object->turn(); }  $driver = new Screwdriver(); turn($driver);  $wheel = new SteeringWheel(); turn($wheel);  $obj = new Object(); // any object that does not implement Turnable turn($object); // ERROR! 

    PHP does permit you to use a type hint for the parameter, and the type hint can be an interface name instead of a class name. So you can be sure that if the $object implements the Turnable interface, then it must have the turn() method. Each class that implements Turnable can do its own thing to accomplish turning.

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

Sidebar

Related Questions

I have some data like this: 1 2 3 4 5 9 2 6
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a JSP page retrieving data and when single or double quotes are
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I've got a string that has curly quotes in it. I'd like to replace
I have text I am displaying in SIlverlight that is coming from a CMS
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
I am trying to loop through a bunch of documents I have to put

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.