Jump to content

Need help assigning a value to a property


Go to solution Solved by kicken,

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.

Edited by Strider64
<?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. =)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.