Page 1 of 1

Class and Functions.

Posted: Sun Jan 26, 2003 6:34 pm
by evilcoder
I have a class called : Auth

inside it i have 3 functions:

Make_Pass
Signup
Activate

Now a user enters all the information we require: Username, Email address

However, signup handles EVERYTHING, all the database work, but i need to generate a password inside that function, how do i call Make_Pass() inside Signup() ?

Thanks.

Posted: Sun Jan 26, 2003 7:13 pm
by Stoker
Your are looking for $this

Code: Select all

<?php
 class test {

   function test () {
      # Initialize
      $this->setbeer(); # Set the default (by calling a function of its own instance)
   }

   function setbeer ($type='Sam Adams') {
      $this->beer = $type;
   }

   function whatbeer (
       return $this->beer;
   }

 }

 $mybeer = new test();
 echo $mybeer->whatbeer(); // Sam Adams

 $mybeer->setbeer('Killians Red');
 echo $mybeer->whatbeer(); // Killians red

?>
Read the PHP manual on OO basics for PHP..
BTW, using OO with PHP chewes quite a bit of resources, I don't really see any point in doing so unless you are building some dynamic object hierarchy system.. (It is very common to use for namespace-purposes, I believe it is much more efficient just prefixing functions/vars and using arrays for multiple instancing)..

?>

Posted: Sun Jan 26, 2003 7:33 pm
by evilcoder
I dont understand your explanation so i'll be checking out the manual.

Yes, OO is needed for my project, as it is a dynamic user-authentication system.

Posted: Sun Jan 26, 2003 8:02 pm
by Stoker
$this points to the current instance of your class, so youcan call it with $this->Make_Pass()

Posted: Sun Jan 26, 2003 11:08 pm
by evilcoder
does it work the same fo calling a function inside another function?

Posted: Sun Jan 26, 2003 11:30 pm
by Stoker
that is what it is for, within a class you can only have code in functions, doing something within your own instance require use of $this->functionname() or $this->variablename

You can also call the function statically in the class using :: , by doing so that function can not access anything else within the instance it was called from..

In your case you would save resources by keeping the make_pass function outside the class, unless it (that function) needs to access any other (self or inherited) functions, variables or child objects.

Basically, if you are used to VB-objects, self. is the same as $this->

Edit/Add: Oops, correction: VB users: Me. (not self.) is the same as $this-> , some other language(s) uses self but I don't recall which now (Delphi?), havent used those for quite some time :)