Two __constructs with one variable passed

Sorry if this is hard to read. Due to Internet issues I am unable to connect from my desktop until my provider gets off their butt to fix their end. I am writing this from my iPhone.

Okay, so, I have a class I am defining and I need two constructs. In one construct, I am passing an integer to the construct. In the other, I am passing a string. The issue is how I define these as the constructs look the same:

function __construct($someInt){
// code goes here
}

function __construct($someStr){
// code goes here
}

If I create an object attempting to pass an integer, how does the compiler know which construct to use? It seem I can’t typecast by declaring the types in the constructs (in other words I can’t say function __construct((string)$someString) as it throws an error. How do I create two different constructs with the same number of variables being passed to it?

On function, the use is_numeric to see if ones an Integer or string then pass over to what scrpit you need?

This is what I assumed but seems to me to almost negate the reason I am making constructors to begin with. One construct with two “if” outcomes makes code less readable to Meghan to have two constructs with different types passed. Too much C++ and Java in my past.

This is what I would do:

[php]

function functionName($var){

if (is_numeric($var)){

          //Do something with Int

 } else {

         //Do something with string

}

}

[/php]

Or have I missed the point?

You haven’t missed the point. This is how I’ve coded it now, but coming from my previous experiences, I could code my constructs and they would be differentiated by what the type is of the variables being passed.

If it works then thats what works. Each language is different even though PHP is based on C. I don’t think there is away of having two of the same function and differentiating between then depending on type. For what its worth what you and I have come up with isn’t bad ,wrong or terrible. Im sure someone will come along and say im wrong… xD

Sponsor our Newsletter | Privacy Policy | Terms of Service