Solved

allow two classes to be parameters to a function

Posted on 2014-01-16
4
251 Views
Last Modified: 2014-01-17
Is there a way to add two classes of variables

//must be from employee class
    function addEmployee(Employee $e) {


//allow employee or intern
    function addEmployee(Employee,Intern $e) {
0
Comment
Question by:rgb192
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39787347
I don't think PHP type hinting allows for multiple hints (full disclosure: I have never tested this).  And that's probably a good thing.  It's like multiple interitance -- just don't do that!
0
 
LVL 34

Expert Comment

by:Slick812
ID: 39787396
greetings, you now can have conditional method parameters as you have in your first code, , HOWEVER, you can not make a parameter conditional for several "Types" (Classes in your case). You are suggesting a almost sure to fail or give trouble multiple type acceptance. But in PHP there are ways to Check the Class Type with  get_class($abz)  ..

public function addEmployee($e) {
if (get_class($abz) == 'Employee') {
   // RUN code here for Employee Class Object
    }
    elseif (get_class($abz) == 'Intern')
    {
   // RUN code here for Intern Class Object
    }
    else
    // Call EXCEPTION HERE For method failure
}

if you are using Sub-Classes and the methods are the same you can test with -
if (is_subclass_of($abz, 'Products')) // success do code, , else exception
0
 
LVL 35

Accepted Solution

by:
gr8gonzo earned 500 total points
ID: 39788664
Define a base class and make Employee and Intern extend from it. Then simply use the base class type as the type hint:

<?php

// Base type
class CompanyPerson
{
}

// Extended type
class Employee extends CompanyPerson
{
  public $salary = 100000;
}

// Extended type
class Intern extends CompanyPerson
{
  public $salary = 0;
}

class PublicPerson
{
	public $salary = 10;
}

// Instances
$Employee = new Employee();
$Intern = new Intern();
$PublicPerson = new PublicPerson();

// Now pass in our instances...
EchoSalary($Employee); // Employee => 100000
EchoSalary($Intern); // Intern => 0
EchoSalary($PublicPerson); // ERROR!

function EchoSalary(CompanyPerson $cp)
{
	echo get_class($cp) . " => " . $cp->salary . "\n";
}

?>

Open in new window


Note that typed parameters in PHP isn't all that useful just yet, in my opinion. It's more useful when you are working with a compiled, strong-typed language like C# or Java where you can do method overloading.
0
 

Author Closing Comment

by:rgb192
ID: 39789954
Extending allows answer.  Thanks
0

Featured Post

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …

623 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