We have a vektortest.php in which we are said to include Vektor.php-class and in it there are methods for randomizing (randomVektor) , sorting (sortVektor) and saving these to $int_vektor.
Our code looks like this:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"xml:lang="sv" >
<head>
<title></title>
</head>
<body>
<?php
include "Vektor.php";
$var_vek = new Vektor();
$var_random->randomVektor();
echo ($int_vektor);
$var_sort->sortVektor();
echo ($int_vektor);
?>
</body>
</html>
the error message we get is:
Notice: Undefined variable: var_random in C:\wamp\www\vektortest.php on line 17
Fatal error: Call to a member function randomVektor() on a non-object in C:\wamp\www\vektortest.php on line 17
Whats wrong?
/Manne
Trouble to instance object of a class. Prob very easy solving
Started by Darkone85, Apr 19 2010 01:54 AM
9 replies to this topic
#1
Posted 19 April 2010 - 01:54 AM
|
|
|
#2
Posted 19 April 2010 - 02:20 AM
Have you actually created Vektor objects $var_random and $var_sort in the Vektor.php file?
#3
Posted 19 April 2010 - 02:27 AM
Hmm... is that necessary? The methods we are calling are already in Vektor.php and since we made an instance of it in vektortest, shouldnt we be able to just use it as weve done?
#4
Posted 19 April 2010 - 02:58 AM
Is the code you've posted not vektortest.php? Or is it an include (I don't see an include for it anywhere)?
#5
Posted 19 April 2010 - 03:02 AM
the code i posted is vektortest.php, correct. weve included Vektor.php in it which have the randomVektor and sortVektor methods in it.
Im sorry for noobing, we just started this course, just done with java :S
Im sorry for noobing, we just started this course, just done with java :S
#6
Posted 19 April 2010 - 03:15 AM
If this is the case, $var_random and $var_sort are just empty variables. If these methods are members of your class then you'll possibly need to call them from $var_vek. It's hard to know without seeing the source for Vektor.php
#7
Posted 19 April 2010 - 05:44 AM
I Lol'd so hard. Yeah I got that finally and the rest to work too want me to post it all? Thanks for your help man
#8
Posted 19 April 2010 - 05:58 AM
Yeah go ahead, I'm still confused as to what was going on in the mystery include file lol.
#9
Posted 20 April 2010 - 03:59 AM
My final vektortest.php:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"xml:lang="sv" >
<head>
<title></title>
</head>
<body>
<?php
include "Vektor.php";
$var_vek = new Vektor();
$var_vek->randomVektor();
echo ($var_vek->formattedVektor());
$var_vek->sortVektor();
echo ($var_vek->formattedVektor());
?>
</body>
</html>
And the included Vektor.php:
<?php
class Vektor
{
//Privat attribut för klassen Vektor.
//$int_vektor kommer att innehåller vektorn.
private $int_vektor;
//Konstruktor för klassen Vektor.
//Skapar 100 platser (0-99) för $int_vektor och tilldelar varje plats värdet noll (0).
function __construct()
{
//array array_fill ( int $start_index , int $num , mixed $value )
$this->int_vektor = array_fill(0, 100, 0);
}
//Destruktor för klassen Vektor.
//Frigör tar bort $int_vektor.
function __destruct()
{
unset($this->int_vektor);
}
//Metoden string2Vektor() skapar och returnerar en vektor av strängar.
//Inkommande parameter skall vara en sträng bestående av siffror och kommatecken (,).
//Vektorn av strängar tilldelas $int_vektor.
public function string2Vektor($inString)
{
$this->int_vektor = explode(",", $inString);
}
//Metoden vektor2String() skapar en sträng av en vektor.
//Strängen som returneras består av siffor och kommatecken (,).
public function vektor2String()
{
return implode(",", $this->int_vektor);
}
//Metoden setVektor() tilldelar $int_vektor en ny vektor.
//Inkommande parameter är den nya vektorn.
public function setVektor($inVektor)
{
$this->int_vektor = $inVektor;
}
//Metoden getVektor returnerar aktuell vektor.
public function getVektor()
{
return $this->int_vektor;
}
//Metoden randomVektor() slumpar 100 tal mellan 1 och 1000 och placerar dessa i $int_vektor.
public function randomVektor()
{
for($int_raknare = 0; $int_raknare < 100; $int_raknare++)
{
$this->int_vektor[$int_raknare] = mt_rand(1, 1000);
}
}
//Metoden sortVektor() sorterar talen i $int_vektor i stigande ordning.
public function sortVektor()
{
sort($this->int_vektor);
}
//Metoden formattedVektor() skapar och returnerar en xhtml-tabell innehållande värdena i $_int_vektor.
public function formattedVektor()
{
$str_output = "<table border=\"1\">";
$int_arraycounter = 0;
for($int_outercounter = 1; $int_outercounter <= 10; $int_outercounter++)
{
$str_output .= "<tr align=\"center\">\n";
for($int_innercounter = 1; $int_innercounter <= 10; $int_innercounter++)
{
$str_output .= "<td>".$this->int_vektor[$int_arraycounter]."</td>\n";
$int_arraycounter++;
}
$str_output .= "</tr>\n";
}
$str_output .= "</table>\n";
return $str_output;
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"xml:lang="sv" >
<head>
<title></title>
</head>
<body>
<?php
include "Vektor.php";
$var_vek = new Vektor();
$var_vek->randomVektor();
echo ($var_vek->formattedVektor());
$var_vek->sortVektor();
echo ($var_vek->formattedVektor());
?>
</body>
</html>
And the included Vektor.php:
<?php
class Vektor
{
//Privat attribut för klassen Vektor.
//$int_vektor kommer att innehåller vektorn.
private $int_vektor;
//Konstruktor för klassen Vektor.
//Skapar 100 platser (0-99) för $int_vektor och tilldelar varje plats värdet noll (0).
function __construct()
{
//array array_fill ( int $start_index , int $num , mixed $value )
$this->int_vektor = array_fill(0, 100, 0);
}
//Destruktor för klassen Vektor.
//Frigör tar bort $int_vektor.
function __destruct()
{
unset($this->int_vektor);
}
//Metoden string2Vektor() skapar och returnerar en vektor av strängar.
//Inkommande parameter skall vara en sträng bestående av siffror och kommatecken (,).
//Vektorn av strängar tilldelas $int_vektor.
public function string2Vektor($inString)
{
$this->int_vektor = explode(",", $inString);
}
//Metoden vektor2String() skapar en sträng av en vektor.
//Strängen som returneras består av siffor och kommatecken (,).
public function vektor2String()
{
return implode(",", $this->int_vektor);
}
//Metoden setVektor() tilldelar $int_vektor en ny vektor.
//Inkommande parameter är den nya vektorn.
public function setVektor($inVektor)
{
$this->int_vektor = $inVektor;
}
//Metoden getVektor returnerar aktuell vektor.
public function getVektor()
{
return $this->int_vektor;
}
//Metoden randomVektor() slumpar 100 tal mellan 1 och 1000 och placerar dessa i $int_vektor.
public function randomVektor()
{
for($int_raknare = 0; $int_raknare < 100; $int_raknare++)
{
$this->int_vektor[$int_raknare] = mt_rand(1, 1000);
}
}
//Metoden sortVektor() sorterar talen i $int_vektor i stigande ordning.
public function sortVektor()
{
sort($this->int_vektor);
}
//Metoden formattedVektor() skapar och returnerar en xhtml-tabell innehållande värdena i $_int_vektor.
public function formattedVektor()
{
$str_output = "<table border=\"1\">";
$int_arraycounter = 0;
for($int_outercounter = 1; $int_outercounter <= 10; $int_outercounter++)
{
$str_output .= "<tr align=\"center\">\n";
for($int_innercounter = 1; $int_innercounter <= 10; $int_innercounter++)
{
$str_output .= "<td>".$this->int_vektor[$int_arraycounter]."</td>\n";
$int_arraycounter++;
}
$str_output .= "</tr>\n";
}
$str_output .= "</table>\n";
return $str_output;
}
}
?>
#10
Posted 20 April 2010 - 04:01 AM
Dude, use CODE tags!
Like this!


Sign In
Create Account


Back to top









