Solved

allow two classes to be parameters to a function

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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 …

679 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