Solved

allow two classes to be parameters to a function

Posted on 2014-01-16
4
243 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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 and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

734 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