Class and Functions.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
evilcoder
Forum Contributor
Posts: 345
Joined: Tue Dec 17, 2002 5:37 am
Location: Sydney, Australia

Class and Functions.

Post 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.
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post 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)..

?>
evilcoder
Forum Contributor
Posts: 345
Joined: Tue Dec 17, 2002 5:37 am
Location: Sydney, Australia

Post 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.
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

$this points to the current instance of your class, so youcan call it with $this->Make_Pass()
evilcoder
Forum Contributor
Posts: 345
Joined: Tue Dec 17, 2002 5:37 am
Location: Sydney, Australia

Post by evilcoder »

does it work the same fo calling a function inside another function?
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post 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 :)
Post Reply