Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Konrad Pilch
Konrad Pilch
2,435 Points

How to assign in the .. (Read more)

HI,

{
  private $_name;
  private $_job;
  private $_age;

  public function __construct($name, $job, $age)
  {
      $this->_name = $name;
      $this->_job = $job;
      $this->_age = $age;
  }

  public function changeJob($newjob)
  {
      $this->_job = $newjob;
  }

  public function happyBirthday()
  {
      ++$this->_age;
  }
}

I do understand this. Now, why is there a private ? does thath means only the person can edit it ? or what is this private? when would i use private or public ?

ALso, there is private and $ name, how would i assign a $ _ POST [ ' username ' ] ; ? to that?

so when somebody register, how would i assign that to this?

also when somebody logs in, how would i assign that to this? im tryin gto understand how to make a login and user OOP good code , i do understand now more, but i still don tget how do i assing values to this,i dont mean $ this -> no. I mean THIS MEANING THIS CODE private $ age etc..

2 Answers

private, protected, and public have to do with the availability of the variable outside the class. Public is available anywhere in the code. Protected is available within the class tree, parent and child classes can see it. Private is only in the class itself.

Konrad Pilch
Konrad Pilch
2,435 Points

So when making a user template with php, it would look like private?

ANd how can i assign some data to it ? i mean , when taking the input that the user has entered, and submit it with it !isset etc.. , put it in db, and then make a user profile, how would i put this in that?

Keep in mind that I am just starting to understand OOP, so I will answer to the best of my ability but I am sure there are more qualified people to answer.

Set up your class and the methods (functions) you want.

<?php
class fun {

private $user;

function doStuff ($userName) {
  $this->user = $userName;
  do your thing;
  return 'Your thing is done';
}
}

$f = new fun;
$f -> doStuff('variable with info');

I believe this would take your input userName and assign it to user for use in the class. Your execution code is in the class and it does its thing.