Re: problem with my class

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



2009/4/16 abdulazeez alugo <defatigue@xxxxxxxxxxx>:
>
>
>> Date: Thu, 16 Apr 2009 14:43:46 +0100
>> From: luke@xxxxxxxxxxxxxx
>> To: php@xxxxxxxxxxxxxx
>> CC: ro0ot.w00t@xxxxxxxxxxxxxx; php-general@xxxxxxxxxxxxx
>> Subject: Re:  problem with my class
>>
>> On Thu, Apr 16, 2009 at 2:41 PM, Thijs Lensselink
>> <php@xxxxxxxxxxxxxx>wrote:
>>
>> > Luke wrote:
>> > > 2009/4/16 Jan G.B. <ro0ot.w00t@xxxxxxxxxxxxxx>
>> > >
>> > >> 2009/4/16 Luke <luke@xxxxxxxxxxxxxx>:
>> > >>> Hi guys,
>> > >>>
>> > >>> I've been learning about object oriented programming and I wrote
>> > >>> this
>> > >> test
>> > >>> class but it doesn't seem to be working, it throws no errors but
>> > doesn't
>> > >>> insert anything to the database either. I have made sure that the
>> > >>> data
>> > >> being
>> > >>> sent when the class is instantiated is valid.
>> > >>>
>> > >>> I'm probably missing something simple here...
>> > >>>
>> > >> Are you actually calling your public function?
>> > >>
>> > >> $x = new RecipeCreator('a', 'b', 'c');
>> > >> $x->saveRecipe();
>> > >>
>> > >> You might want to insert some error reporting...
>> > >>
>> > >> echo mysql_error(); and alike
>> > >>
>> > >>
>> > >> Byebye
>> > >>
>> > >>
>> > >>
>> > >>
>> > >>> I have already
>> > >>>
>> > >>> class RecipeCreator
>> > >>> {
>> > >>> private $rtitle;
>> > >>> private $problem;
>> > >>> private $solution;
>> > >>>
>> > >>> function __construct ($t, $p, $s)
>> > >>> {
>> > >>> if(!isset($t, $p, $s))
>> > >>> {
>> > >>> throw new Exception ('Missing parameters for
>> > >>> __construct, need $title $problem and $solution');
>> > >>> }
>> > >>>
>> > >>> $this->rtitle = mysql_real_escape_string($t);
>> > >>> $this->problem = mysql_real_escape_string($p);
>> > >>> $this->solution = mysql_real_escape_string($s);
>> > >>> }
>> > >>>
>> > >>> public function saveRecipe()
>> > >>> {
>> > >>> $query = "INSERT INTO recipe (title, problem, solution)
>> > >>> VALUES ('".$this->rtitle."',
>> > >>>
>> > >>> '".$this->problem."',
>> > >>>
>> > >>> '".$this->solution."')";
>> > >>> mysql_query($query);
>> > >>> }
>> > >>> }
>> > >>>
>> > >>> Many thanks,
>> > >>> Luke Slater
>> > >>>
>> > >> --
>> > >> PHP General Mailing List (http://www.php.net/)
>> > >> To unsubscribe, visit: http://www.php.net/unsub.php
>> > >>
>> > >>
>> > >
>> > > Yes I am doing that. The query seems to be going through all right too
>> > > I
>> > > tested the syntax in mysql query browser and tried mysql_error() but
>> > there
>> > > was nothing.
>> > > I think there's an issue with the __construct because when I wrote a
>> > method
>> > > in there to return one of the properties of the function, it turned up
>> > > blank! As I said before the variables I'm passing in are definitely
>> > valid...
>> > >
>> >
>> > And what happens when you instantiate your object without parameters?
>> >
>> > $foo = new RecipeCreator();
>> >
>> > It should throw and exception. This way you at least know if your
>> > constructor is functioning properly.
>> >
>> > And take a look at the message you throw when the parameters are not
>> > set. The parameters are named $t, $p, $s but the message in the throw
>> > statement uses $title, $problem, $solution
>> >
>> > did you try echoing out the query and run it from phpMyAdmin or
>> > something similar?
>> >
>>
>> I changed the variables to $t $p and $s because I thought by naming them
>> with $title $problem and $solution may have been getting them mixed up
>> somehow.
>>
>> I figured out the problem, which was that I was actually calling the
>> saveRecipe() method without the parentheses.
>>
>> Too long programming in Perl -.-
>>
>> Thanks for your help,
>>
>> Luke Slater
>
> Hi Luke,
> class RecipeCreator
>  {
>  private $rtitle;
> private $problem;
> private $solution;
>
> function __construct ($t, $p, $s)
>  {
> if(!isset($t, $p, $s))
>  {
>  throw new Exception ('Missing parameters for
>  __construct, need $title $problem and $solution');
>  }
> I think the problem was with the $title. As above, you defined the private
> variable $rtitle but you were trying to call the variable later as $title.
>


The problem was already solved. But I feel like mentioning, that the
Exeption is just "naming" the variables with their content...
the class reads the vars as $t, $p and $s and it dowsn't matter at all
if you call ist with new foo($BAR, $whatever,
$somethingveryverydifferent), as once in the class only the VALUES
count. and yey're being assigned to the function vars $p, $t, $s,
which are being set to PRIVATE vars $rtitle, $problem etc.
Conclusion: The variable handling has no errors!

byebye



> Cheers.
> Alugo Abdulazeez.
>
>
> ________________________________
> Get news, entertainment and everything you care about at Live.com. Check it
> out!

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux