Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

noob question - undefined variable

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

noob question - undefined variable

Postby louism » Wed Apr 17, 2013 12:18 pm

Hi, Can anybody guide me in the right direction here? It's my first time working with classes etc and I can't for the life of me see where I'm going wrong.
In the example below, I'm passing it "600" which is the $AgeInMonths and it is returning "22.4" which is correct. So I'm not sure why these errors are occurring.
Any pointers would be appreciated.
Thank You, Louis

I'm getting an error of:
Warning: Missing argument 1 for CommutationFactor::CommutationFactor(), called in \testsite\time.php on line 154 and defined in \testsite\class.CommutationFactor.php on line 3

Notice: Undefined variable: AgeInMonths in \testsite\class.CommutationFactor.php on line 5
Notice: Undefined variable: AgeInMonths in \testsite\class.CommutationFactor.php on line 8
Notice: Undefined variable: AgeInMonths in \testsite\class.CommutationFactor.php on line 9
Notice: Undefined variable: AgeInMonths in \testsite\class.CommutationFactor.php on line 10
Notice: Undefined variable: AgeInMonths in \testsite\class.CommutationFactor.php on line 11

yada yada for each case statement


time.php
Code: Select all
// get commutation factor
$AgeInMonths = "600";     //  <<< I've just added this to test
$AgeInYears = floor($AgeInMonths/12);    //  <<< for another function
$oCommutationFactor = new CommutationFactor();
$CommutationFactor = $oCommutationFactor->CommutationFactor($AgeInMonths); 


class.CommutationFactor.php
Code: Select all
<?php
class CommutationFactor { 
  function CommutationFactor($AgeInMonths){ 
    switch ($AgeInMonths) {
      case 600:
        $sCommutationFactor = "22.4";
        break;
      case 601:
      case 602:
      case 603:
      case 604:
        $sCommutationFactor = "22.3";
        break;
      case 605:
      case 606:
      case 607:
      case 608:
        $sCommutationFactor = "22.2";
        break;
      case 609:
      case 610:
      case 611:
      case 612:
        $sCommutationFactor = "22.1";
        break;
      case 613:
      case 614:
      case 615:
      case 616:
        $sCommutationFactor = "22.0";
        break;
      case 617:
      case 618:
      case 619:
      case 620:
        $sCommutationFactor = "21.9";
        break;
      case 621:
      case 622:
      case 623:
      case 624:
        $sCommutationFactor = "21.8";
        break;

      default:
        $sCommutationFactor = "Error Message";
        break;
   }   
    return $sCommutationFactor;
   
  }
  }
?>
louism
New php-forum User
New php-forum User
 
Posts: 5
Joined: Wed Apr 17, 2013 11:55 am

Re: noob question - undefined variable

Postby seandisanti » Wed Apr 17, 2013 1:59 pm

Look at the error, and break it down...
Warning: Missing argument 1 for CommutationFactor::CommutationFactor()

It's saying that the function CommutationFactor, in the CommutationFactor class is being called without a required arguments...... but where is it being called from you ask?
called in \testsite\time.php on line 154

and where is the function defined so that you can see what arguments are required?
and defined in \testsite\class.CommutationFactor.php on line 3
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: noob question - undefined variable

Postby Strider64 » Wed Apr 17, 2013 5:31 pm

I just wanted to add if it wasn't you intention to call your function the same as your class you can always call you function something else and pass the argument in the way that you have it. I'm guessing you came from programing in Actionscript?
User avatar
Strider64
New php-forum User
New php-forum User
 
Posts: 69
Joined: Sat Mar 23, 2013 8:24 am

Re: noob question - undefined variable

Postby louism » Wed Apr 17, 2013 6:52 pm

Thanks for the answers. No, this is my first program and I quite like php even though it can be a bit of a pain. The naming was just me, sort of making it easy for myself although it may not seem it by somebody with experience.
With regards to the clues from seandisanti, I can count and I've looked at those lines again and again but cannot see it. Hence why I'm here.

time.php
LINE 154 = $oCommutationFactor = new CommutationFactor();

CommutationFactor.class
LINE 3 = function CommutationFactor($AgeInMonths){

If you look at the code, I know that "600" is being passed from "time.php" to CommutationFactor.class as $AgeInMonths and the class is returning the appropriate value eg Case 22.4 back to time.php as it should.

So, I'm totally stuck as to why it's returning these errors. I could understand it if "600" or a figure wasn't being passed but it is because the function would not return the correct value if it wasn't.
louism
New php-forum User
New php-forum User
 
Posts: 5
Joined: Wed Apr 17, 2013 11:55 am

Re: noob question - undefined variable

Postby johnj » Wed Apr 17, 2013 10:16 pm

Please read about class constrctors from the php manual on php.net.

You can also get a feel of constructors from here http://phillipnb.wordpress.com/2010/08/ ... construct/

In your class, inside the class your constructor is written in such a way that it is expecting one parameter, which is $AgeInMonths. And, then when you instantiate your class using the keyword 'new', you are not passing this parameter value.
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: noob question - undefined variable

Postby louism » Thu Apr 18, 2013 12:00 am

Hi Johnj,
thanks for the reply. Call me stupid or something but I still can't see what the problem is.
I'm looking at the error etc and know what it's telling me, but the thing is working.

The CommutationFactor.class is expecting a value for $AgeInMonths
It is getting this value which is currently set to "600" but will get any value that I put into it.
For the value "600", the function is meant to return "22.4" which it is doing which is why I can't understand why the debugger is showing the error up.

On top of that, if I run it on one webserver (centos6.3_x64 with php5.3), it runs without error.
If i run it on another (centos6.3_x64 with php5.3), it does not run at all. Very strange.
louism
New php-forum User
New php-forum User
 
Posts: 5
Joined: Wed Apr 17, 2013 11:55 am

Re: noob question - undefined variable

Postby seandisanti » Thu Apr 18, 2013 7:26 am

Your function with the same name as the class is being treated as a constructor. A simple explanation is that a constructor is the function run when you instantiate the class. so when you call :

LINE 154 = $oCommutationFactor = new CommutationFactor(); It's calling the CommutationFactor METHOD within the class of the same name. You've not defined a default value for the $AgeInMonths so calling without passing a value for it does not work correctly. It's like asking a child "what's 2 plus?" If you leave off an argument or an operand, the computer doesn't know how to handle it.
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: noob question - undefined variable

Postby louism » Thu Apr 18, 2013 11:50 am

Thank you seandisanti. That's the sort of explanation I was looking for. I actually added =null and it sorted it but it was your post that pointed me in the right direction.
I take it my coding is poor by calling the class & the function the same name?
louism
New php-forum User
New php-forum User
 
Posts: 5
Joined: Wed Apr 17, 2013 11:55 am

Re: noob question - undefined variable

Postby seandisanti » Thu Apr 18, 2013 11:57 am

It's a better practice to use the magic method __construct() as a constructor, and it helps readability. I'm glad I could help.
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: noob question - undefined variable

Postby johnj » Thu Apr 18, 2013 7:38 pm

Yes, the constructor in php is written using __construct(). There is nothing wrong in using the class name but in php we usually use the magic method __ construct()
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: noob question - undefined variable

Postby louism » Thu Apr 18, 2013 9:54 pm

Thank you very much guys. Much appreciated.
louism
New php-forum User
New php-forum User
 
Posts: 5
Joined: Wed Apr 17, 2013 11:55 am

Re: noob question - undefined variable

Postby seandisanti » Fri Apr 19, 2013 12:43 am

no problem, just glad to help.
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm


Return to PHP coding => General

Who is online

Users browsing this forum: Bing [Bot] and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.