Solved

allow two classes to be parameters to a function

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

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 33

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 34

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
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.

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now