why this error?

This is a discussion on why this error? within the PHP Language forums, part of the PHP Programming Forums category; This is my first "class" algorithm: when i execute it, i get this error: <<Fatal error: ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-28-2007
vinnie
 
Posts: n/a
Default why this error?

This is my first "class" algorithm: when i execute it, i get this
error:

<<Fatal error: Call to undefined function: sum() in d:\class.php on
line 20>>

what's wrong? The class has the function, so why it says undefined?


<?php
print("first time class");
class somma
{
var $uno=10;
var $due=15;
var $tre=15;
function sum($uno, $due, $tre)
{
$totale = $uno + $due + $tre;
return($totale);
}
function sum_2()
{
$totale = $this->sum($this->uno, $this->due, $this->tre);
return $totale;
}
}
$sum=sum(34,40);
print("$sum");
?>

Reply With Quote
  #2 (permalink)  
Old 05-28-2007
purcaholic
 
Posts: n/a
Default Re: why this error?

On 28 Mai, 23:36, vinnie <centro.ga...@gmail.com> wrote:
> This is my first "class" algorithm: when i execute it, i get this
> error:
>
> <<Fatal error: Call to undefined function: sum() in d:\class.php on
> line 20>>
>
> what's wrong? The class has the function, so why it says undefined?
>
> <?php
> print("first time class");
> class somma
> {
> var $uno=10;
> var $due=15;
> var $tre=15;
> function sum($uno, $due, $tre)
> {
> $totale = $uno + $due + $tre;
> return($totale);
> }
> function sum_2()
> {
> $totale = $this->sum($this->uno, $this->due, $this->tre);
> return $totale;
> }
> }
> $sum=sum(34,40);
> print("$sum");
> ?>


Hi vinnie,

you have declared sum function inside the somma class, and you're
trying to use it outside the class scope. This will not work.

First you should create an instance of somma and then call the sum
function using "->" operator.
Example:
[snip]
$mySomma = new somma();
$sum=$mySomma->sum(34,40);
print("$sum");
[/snap]

purcaholic

Reply With Quote
  #3 (permalink)  
Old 05-28-2007
farrishj@gmail.com
 
Posts: n/a
Default Re: why this error?

On May 28, 4:36 pm, vinnie <centro.ga...@gmail.com> wrote:
> This is my first "class" algorithm: when i execute it, i get this
> error:
>
> <<Fatal error: Call to undefined function: sum() in d:\class.php on
> line 20>>
>
> what's wrong? The class has the function, so why it says undefined?
>
> <?php
> print("first time class");
> class somma
> {
> var $uno=10;
> var $due=15;
> var $tre=15;
> function sum($uno, $due, $tre)
> {
> $totale = $uno + $due + $tre;
> return($totale);
> }
> function sum_2()
> {
> $totale = $this->sum($this->uno, $this->due, $this->tre);
> return $totale;
> }
> }
> $sum=sum(34,40);
> print("$sum");
> ?>


When using objects, you have to "create" them using the "new" keyword.
So consider a class a demarcation of what makes up an object, with
references to that demarcation being stored in the $variable that was
used to create the object.

Consider:

<code>
<h4>Somma object test class</h4>
<p>Print a sum from the Somma class</p>
<pre>
<?php

class Somma {
var $totale = 0;
var $tre = 15;
// Php4 constructor, "makes" object and
// returns a reference to the object in
// memory, to be stored in the $variable
// that is = new Somma($uno, $due);
function Somma($uno, $due) {
$this->totale = $uno + $due + $this->tre;
}
}
// $variable stores reference to object in
// memory
$variable = new Somma(34,40);
echo $variable->totale . "\n\n";
// Let's look at the object
print_r($variable);

?>
</pre>
</code>

Note the use of $this-> and $variable in the different scopes ($this->
is used inside the class, $variable is used to refer to the object's
methods and properties, which are called $variable->Somma(85,98);

Reply With Quote
  #4 (permalink)  
Old 05-29-2007
Jerry Stuckle
 
Posts: n/a
Default Re: why this error?

vinnie wrote:
> This is my first "class" algorithm: when i execute it, i get this
> error:
>
> <<Fatal error: Call to undefined function: sum() in d:\class.php on
> line 20>>
>
> what's wrong? The class has the function, so why it says undefined?
>
>
> <?php
> print("first time class");
> class somma
> {
> var $uno=10;
> var $due=15;
> var $tre=15;
> function sum($uno, $due, $tre)
> {
> $totale = $uno + $due + $tre;
> return($totale);
> }
> function sum_2()
> {
> $totale = $this->sum($this->uno, $this->due, $this->tre);
> return $totale;
> }
> }
> $sum=sum(34,40);

^^^^^^^^^^^^^^^^
> print("$sum");
> ?>
>


I've marked the failing line. This line tries to call a global sum()
function with two arguments. This is a valid error; no such function
exists in the system, and you haven't defined one in the global scope.

I'm not entirely sure what you're trying to do here, so I hesitate to
give you more advice and possibly lead you astray.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 05:34 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0