Sam_B Posted December 13, 2014 Share Posted December 13, 2014 First, I apologize if this is the wrong venue, but it seemed the best place to ask question about code. The code below is very simple most likely, but I am having the hardest time visualizing how it's run. It would be most helpful if someone could point out the error of my thinking that would be awesome. This is literally the first php lesson I have ever taken. I'm a very visual, hands on kind of learner. It would be most appreciated to get direction into the best way to learn PHP as a visual learner. Thanks! <?php // I'm following an example online and need an explanation to this. This first part is creating // a class called User. class User { // This is a variable set to public. Now in the lesson I discovered public and private. Why is // public and private important? Could the code just run on $age? public $age; // This is a public function called __construct. Could I name this anything and the function // would still work? What is important about __? public function __construct($age) { // Ok, why is $this used? Is it correct in assuming that any word with a "$" assigned to it in // this class will refer to age? $this->age = $age; } // This is the 3rd function called getAge(). Why is getAge() blank? public function getAge() { echo $this->age; } } // Shoudln't this be at the top? And isn't there a cleaner way to output the age of a new user? // This seems to take up way too much coding just to display the name of a variable? $brad = new User(31); // I'm so confused. $brad->getAge(); Quote Link to comment https://forums.phpfreaks.com/topic/293077-beginner-php-coder/ Share on other sites More sharing options...
php_bad_boy Posted December 13, 2014 Share Posted December 13, 2014 The code defines a class called User and then creates an object instance of that class called $brad. Finally it calls the getAge() method of the $brad object and the result is that it prints the number 31. A class is a template which is used to create objects. Objects are like bubbles which encapsulate data and functions (also called methods). The data and functions can be defined as public which means they can be accessed from anywhere in your program, or as private which means they are hidden away and can only be accessed by the object itself. The __constructor() function is a special function, it is always called when a new object is created. The getAge() function is a normal function, it only runs when you decide to call it from somewhere else in your program (the final line). The $this variable is a way for an object to refer to itself. What's the point of all this? Say you wanted to have several users, you've now built the 'User' class template and it's really easy to create them all. For example let's create Bob who is 27 and Jane who is 33: $bob = new User(27); $jane = new User(33); Try this video where some of the concepts are explained: Quote Link to comment https://forums.phpfreaks.com/topic/293077-beginner-php-coder/#findComment-1499521 Share on other sites More sharing options...
Strider64 Posted December 13, 2014 Share Posted December 13, 2014 There also a lot of neat things you can do with a classes, such as this for example: Let's say you have a class called Member.php <?php # Member class - Store user info and functions to access/control the flow of data. class Member { // The member attributes (variables) containing required and optional information. // The attributes must correspond to the database table columns: private $id = NULL; private $userType = NULL; // Required private $username = NULL; // Required private $email = NULL; // Required private $pass = NULL; // Required private $fullName = NULL; private $validation_code = NULL; private $address = NULL; private $city = NULL; private $state = NULL; private $zipCode = NULL; // Method returns the user ID: public function getId() { return $this->id; } // Grab the user's username: public function getUsername() { return $this->username; } // Grab the user's full name: public function getFullName() { return $this->fullName; } // Grab the password: public function getPass() { return $this->pass; } public function getUserType() { return $this->userType; } // Clear the password once user is logged in: public function clearPass() { $this->pass = NULL; } public function getEmail() { return $this->email; } // Method returns a Boolean if the user is an administrator: public function isAdmin() { return ($this->userType == 'author'); } public function isSysop() { return ($this->userType == 'sysop'); } public function isNewUser() { return ($this->userType == 'public'); } // Method returns a Boolean indicating if the user is an administrator // or if the user is the original author of the provided page: public function canEditPage(Page $page) { return (($this->isAdmin() || ($this->id == $page->getCreatorId()))); } // Method returns a Boolean indicating if the user is an administrator or an author: public function canCreatePage() { return ($this->isAdmin() || $this->isSysop()); } } The you can simply do the following to put the pull a corresponding record from a database table: // 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'])); /* The following fetches the class Member and creates an instance $stored_user_data */ $stmt->setFetchMode(PDO::FETCH_CLASS, 'Member'); $stored_user_data = $stmt->fetch(); Then for example you can check the user's input against the database table password like this: // Verify Stored Hashed Password against input: if ($stored_user_data) { $result = password_verify($_POST['pass'], $stored_user_data->getPass()); } However, you got to learn how to crawl before you walk, so following php_bad_boy's post would probably be the prudent thing to do. Quote Link to comment https://forums.phpfreaks.com/topic/293077-beginner-php-coder/#findComment-1499525 Share on other sites More sharing options...
hansford Posted December 13, 2014 Share Posted December 13, 2014 private which means they are hidden away and can only be accessed by the object itself. Let's clear this up - I'm sure you know what you meant, but a beginner might not. The object of the class can't even access a private variable - only within the class itself. Quote Link to comment https://forums.phpfreaks.com/topic/293077-beginner-php-coder/#findComment-1499536 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.