Solved

allow two classes to be parameters to a function

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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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 …

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