PHP Calling method inside method

Timothy Coetzee

PHP student here Consider the following 2 methods the 1 method checks if a user exists the other registers a user.

public function is_registered($userEmail) 
{
   $this->email = $userEmail;

   $sql = "SELECT * from users WHERE email = :email";

   $stmnt = $db->prepare($sql);
   $stmnt->bindValue(':email', $userEmail);

   $check = $stmnt->execute();

   if($check >= 1) {
      return true;
   }
   else {
      return false;
   }
}


public function register_user($email, $pword, $firstname, $lastname)
{
   $this->email = $email;

   //call method is_registered inside register user class
   $registered = $this->is_registered($email);

   if($registered == true){
      die("USER EXISTS");
   }
   else {
      //continue registration
   }

 }

Although this might not be the best use case example I basically want to know:

  1. Is "normal / good practice" to call a method inside a method. Another use case might be calling a get_email() method inside a login() method or similar method where you need an email address.

  2. What is the difference if I set a property inside a method or just access the passed parameter directly? Example:

Method with set property

public function is_registered($userEmail)
{
   $this->email = userEmail // SET PROPERTY

   $sql = "SELECT * from users WHERE email = :email";
   $stmnt = $db->prepare($sql);
   $stmnt->bindValue(':email', $userEmail);
}

Method with no set property.

public function is_registered($userEmail) 
{
   //NO SET PROPERTY
   $sql = "SELECT * from users WHERE email = :email";
   $stmnt = $db->prepare($sql);
   $stmnt->bindValue(':email', $userEmail);
}

Hope this makes sense, any help or guidance much appreciated.

Tommaso Belluzzo

On the point of view of OOP, both approaches are kinda weird. Since your User class doesn't seem to be static, and since the e-mail address is one of the major uniqueness discriminant for authentication, your class should be instantiated with a valorized email property that never changes... either upon database extraction (1) or form filling (2):

$user = new User(..., $auth->email); (1)
$user = new User(..., $_POST['registration_mail']); (2)

On the top of that, a method named is_registered should really not mess with assignments. All it should do is to check whether the current User instance class is defined in your database or not.

In your first approach, the risk is to mess up everything. Let's suppose you have the following user:

$user = new User('John', '[email protected]');

Now, let's see what happens when, by mistake, you pass the wrong email as argument:

$result = $user->is_registered('[email protected]');
echo $user->name; // John
echo $user->email // [email protected]

In your second approach, you should not allow the is_registered to accept any argument since, as I explained you before, that property should be defined upon creation to avoid fatal mistakes. Let's bring back the same user we used in the first example:

$user = new User('John', '[email protected]');

Now, let's see what happens when, by mistake, you pass the wrong email as argument ([email protected] is registered, [email protected] isn't):

$result = $user->is_registered('[email protected]'); // false

So my suggestion is, initialize the property upon the creation of the instance and never mess with it. Alternatively, you could implement a static function into a utility class that, for a given email, performs the desired check. But try to make your OOP design as strict as possible.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling PDO inside a method PHP

From Dev

Calling a method inside a method

From Dev

Calling a method inside model

From Java

Calling a method inside another method in same class

From Dev

Calling a method inside another method JavaScript

From Dev

Calling method inside a method in Interface c#

From Dev

C# Calling a Method Inside another Method

From Dev

Flutter calling a method inside the build method

From Dev

Calling the this.method() inside function of another method

From Android

Calling a function inside onRespons method

From Dev

Calling a controller inside a factory method

From

Calling a method inside getDerivedStateFromProps in React

From Dev

Calling self inside a method in smalltalk

From Dev

calling method in another class in php

From Dev

Calling a particular implementation inside a generic method

From Dev

Calling prototype method inside another results in 'is not a function'

From Dev

Calling class method inside string format

From Dev

Calling a method from inside of another class

From Dev

Calling Thread.sleep() inside an asychronous method

From Dev

Calling method on navigation host fragment inside MainActivity

From Java

Calling a static method inside a class in jar file

From Dev

Calling a method with a for loop inside, not updating variables

From Dev

Vue - calling a method inside the vue model

From Dev

Calling Methods Inside Another Method In a Different Class

From Dev

calling a method of a Object inside Session without typecasing

From Dev

calling grandparent method from inside the grandchild component

From Java

Calling a method with parameter inside JSTL loop

From Dev

Is declaring a variable inside of calling a method bad practice?

From Dev

__call__ when calling a method inside the class

Related Related

  1. 1

    Calling PDO inside a method PHP

  2. 2

    Calling a method inside a method

  3. 3

    Calling a method inside model

  4. 4

    Calling a method inside another method in same class

  5. 5

    Calling a method inside another method JavaScript

  6. 6

    Calling method inside a method in Interface c#

  7. 7

    C# Calling a Method Inside another Method

  8. 8

    Flutter calling a method inside the build method

  9. 9

    Calling the this.method() inside function of another method

  10. 10

    Calling a function inside onRespons method

  11. 11

    Calling a controller inside a factory method

  12. 12

    Calling a method inside getDerivedStateFromProps in React

  13. 13

    Calling self inside a method in smalltalk

  14. 14

    calling method in another class in php

  15. 15

    Calling a particular implementation inside a generic method

  16. 16

    Calling prototype method inside another results in 'is not a function'

  17. 17

    Calling class method inside string format

  18. 18

    Calling a method from inside of another class

  19. 19

    Calling Thread.sleep() inside an asychronous method

  20. 20

    Calling method on navigation host fragment inside MainActivity

  21. 21

    Calling a static method inside a class in jar file

  22. 22

    Calling a method with a for loop inside, not updating variables

  23. 23

    Vue - calling a method inside the vue model

  24. 24

    Calling Methods Inside Another Method In a Different Class

  25. 25

    calling a method of a Object inside Session without typecasing

  26. 26

    calling grandparent method from inside the grandchild component

  27. 27

    Calling a method with parameter inside JSTL loop

  28. 28

    Is declaring a variable inside of calling a method bad practice?

  29. 29

    __call__ when calling a method inside the class

HotTag

Archive