Jump to content

Need help assigning a value to a property


eldan88

Recommended Posts

Hey,

 

 I am little bit new to OOP, and I have some difficulties assigning a string to a property through a method, and then calling the property through the method. Below is an example of what I am trying to accomplish, and when I do call the method nothing echo's out.

class test1 {

public $last_name;

public function name() {
	$this->$last_name="scott";
	return $this->last_name;
}
	
}

$name = new test1();
echo $name->name();


<?php

// Using traditional setters and getters:
class User {

  private $username = NULL;
  
  public function getUsername() {
	  return $this->username;
  }
  
  public function setUsername($username) {
        $this->username = $username;
  }
    	
}

$data = new User();

$data->setUsername('Justin Verlander');

echo $data->getUsername();

This uses traditional setters and getters method, but what really neat is that you don't need to use them when pulling from mysql

, for you can do something like this:

    // Check against the database:
    $query = 'SELECT id, userType, username, email, pass, fullName, address, city, state, zipCode FROM users WHERE username=:username';
    $stmt = $pdo->prepare($query);
    $stmt->execute(array(':username' => $_POST['username']));
	 
    
    $stmt->setFetchMode(PDO::FETCH_CLASS, 'User');
	$stored_user_data = $stmt->fetch();

You just have to make sure that the names in the database corresponds to what in the class:

 

For example public username=Null; must have a matching table column username. I also try to keep name my classes that corresponds in what I doing.

<?php

// Using traditional setters and getters:
class User {

  private $username = NULL;
  
  public function getUsername() {
	  return $this->username;
  }
  
  public function setUsername($username) {
        $this->username = $username;
  }
    	
}

$data = new User();

$data->setUsername('Justin Verlander');

echo $data->getUsername();

This uses traditional setters and getters method, but what really neat is that you don't need to use them when pulling from mysql

, for you can do something like this:

    // Check against the database:
    $query = 'SELECT id, userType, username, email, pass, fullName, address, city, state, zipCode FROM users WHERE username=:username';
    $stmt = $pdo->prepare($query);
    $stmt->execute(array(':username' => $_POST['username']));
	 
    
    $stmt->setFetchMode(PDO::FETCH_CLASS, 'User');
	$stored_user_data = $stmt->fetch();

You just have to make sure that the names in the database corresponds to what in the class:

 

For example public username=Null; must have a matching table column username. I also try to keep name my classes that corresponds in what I doing.

 

Thanks a lot. I read through it and it makes a lot of sense. =)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.