TeddyKiller Posted April 6, 2010 Share Posted April 6, 2010 I've been reading up about Classes and Functions more, to enable me to upgrade some of mine to be .. better. Would this work as a class. <?php class myclass { var $myvariable; function echothis() { echo $myvariable; } } $class = new myclass(); $myvariable = 'this is a string'; $class->echothis(); If thats how it works, whats the advantage? When I could simply do.. function echothis($myvariable) { echo $myvariable; } $myvariable = 'this is a string'; echothis($myvariable); I'm trying to expand my knowledge. I know this isn't really a major question, just a bit of .. understanding of classes and functions. Quote Link to comment https://forums.phpfreaks.com/topic/197699-classes-and-functions-help/ Share on other sites More sharing options...
Alex Posted April 6, 2010 Share Posted April 6, 2010 In your example there would be no benefit. If you don't know the benefits of using OOP you shouldn't be using it because you won't be getting anything out of it anyway. You can easily find a lot of material about OOP so if you're really interested I suggest you read up on it. You can start here. Quote Link to comment https://forums.phpfreaks.com/topic/197699-classes-and-functions-help/#findComment-1037543 Share on other sites More sharing options...
trq Posted April 6, 2010 Share Posted April 6, 2010 Your example is a little off anyways.... class myclass { public $myvariable; function echothis() { echo $myvariable; } } $class = new myclass(); $class->myvariable = 'this is a string'; $class->echothis(); But AlexWD is right, something so simple won't really demonstrate the advantages of using objects. Quote Link to comment https://forums.phpfreaks.com/topic/197699-classes-and-functions-help/#findComment-1037547 Share on other sites More sharing options...
TeddyKiller Posted April 6, 2010 Author Share Posted April 6, 2010 Theres alot for me to learn, I'm just trying to pick up on the basics. I'll certainly read what you gave me. I know in my example, it'd be pretty pointless. Though that's where you start, something.. silly, working your way up. Could you give an explanation of public and private functions? Thorpe, thanks for the correction. That alone helps me understand `public`'s inside classes. What are `var`'s then? Theres many things for me to actually read upon. Quote Link to comment https://forums.phpfreaks.com/topic/197699-classes-and-functions-help/#findComment-1037550 Share on other sites More sharing options...
Alex Posted April 6, 2010 Share Posted April 6, 2010 More often than not you can find what you need to know in the manual, and if somewhere else online. http://php.net/manual/en/language.oop5.visibility.php As it states there, public methods and properties are accessible from outside of the object and private one's aren't, basically. class Foo { private $_var = 'value'; public $var = 'value'; public function __construct() { ... } } $instance = new Foo(); echo $instance->var; // Can access this property since it's public echo $instance->_var; // Can't access this property because it's private Quote Link to comment https://forums.phpfreaks.com/topic/197699-classes-and-functions-help/#findComment-1037555 Share on other sites More sharing options...
TeddyKiller Posted April 6, 2010 Author Share Posted April 6, 2010 Yeah.. I understand. Would the same rule apply to private function() and public function() ? Why doesn't this get any data ? I can echo $class->id and get the correct ID, but it doesn't seem to be putting it into the query? class profile { public $id; function user_details($profile, $user) { $query = mysql_query("select * from `users` where `id`='$id'"); $row = mysql_fetch_array($query); echo $row; } } $class = new profile(); $class->id = $_GET['uid']; $class->user_details($profile, $user); I'm just mucking around.. Again, probably another useless technique. Quote Link to comment https://forums.phpfreaks.com/topic/197699-classes-and-functions-help/#findComment-1037557 Share on other sites More sharing options...
trq Posted April 6, 2010 Share Posted April 6, 2010 Internal properties & methods need to be accessed using the $this keyword. $query = mysql_query("select * from `users` where `id`='{$this->id}'"); Because they belong to the current instance of the object. Again, this is all covered in the manual. Quote Link to comment https://forums.phpfreaks.com/topic/197699-classes-and-functions-help/#findComment-1037558 Share on other sites More sharing options...
Alex Posted April 6, 2010 Share Posted April 6, 2010 To access properties and methods from within the class you need to use the $this variable. $this->id $query = mysql_query("select * from `users` where `id`='{$this->id}'"); // or $query = mysql_query("select * from `users` where `id`='" . $this->id . "'"); edit: Beat to it Quote Link to comment https://forums.phpfreaks.com/topic/197699-classes-and-functions-help/#findComment-1037562 Share on other sites More sharing options...
TeddyKiller Posted April 6, 2010 Author Share Posted April 6, 2010 Great stuff. Thorpe, you just didn't put that in the corrected example so.. I was confused. I better get reading, do you know any good PHP books that are worth buying at all? Might be useful to me. http://www.amazon.co.uk/Programming-PHP-Rasmus-Lerdorf/dp/1565926102 Is this any good? Quote Link to comment https://forums.phpfreaks.com/topic/197699-classes-and-functions-help/#findComment-1037564 Share on other sites More sharing options...
trq Posted April 6, 2010 Share Posted April 6, 2010 Building on your example, I can show you one of the benefits of using classes / objects over simple functions. Objects are able to hold state. <?php class profile { private $_result; function getUsers() { if ($result = mysql_query("select name from `users` where `id`='$id'")) { if (mysql_num_rows($result)) { $this->result = $result; return true; } } return false; } public function fetch() { return mysql_fetch_object($this->_result); } } $profile = new profile(); if ($profile->getUsers()) { while ($user = $profile->fetch()) { echo $user->name; } } ?> Its still not really a good example of OOP, but it does however highlight the fact that objects can hold state. Notice how the result of your query is stored internally within $_result? This can't be done with simple functions alone. Quote Link to comment https://forums.phpfreaks.com/topic/197699-classes-and-functions-help/#findComment-1037565 Share on other sites More sharing options...
trq Posted April 6, 2010 Share Posted April 6, 2010 If your looking for a book on php OOP. http://www.amazon.com/PHP-5-Objects-Patterns-Practice/dp/1590593804 You'll want to have a pretty good understanding of PHP already though. Quote Link to comment https://forums.phpfreaks.com/topic/197699-classes-and-functions-help/#findComment-1037566 Share on other sites More sharing options...
TeddyKiller Posted April 6, 2010 Author Share Posted April 6, 2010 My PHP understanding is.. well it's not too bad. I wouldn't say its brilliant. Quote Link to comment https://forums.phpfreaks.com/topic/197699-classes-and-functions-help/#findComment-1037567 Share on other sites More sharing options...
trq Posted April 6, 2010 Share Posted April 6, 2010 My PHP understanding is.. well it's not too bad. I wouldn't say its brilliant. Well, I'd recommend working on hat first. OOP really is a different ballgame. Quote Link to comment https://forums.phpfreaks.com/topic/197699-classes-and-functions-help/#findComment-1037568 Share on other sites More sharing options...
TeddyKiller Posted April 6, 2010 Author Share Posted April 6, 2010 What do you mean "working on hat" thorpe? <?php class profile { private $_result; function getUsers() { if ($result = mysql_query("select name from `users` where `id`='$id'")) { if (mysql_num_rows($result)) { $this->result = $result; return true; } } return false; } public function fetch() { return mysql_fetch_object($this->_result); } } $profile = new profile(); if ($profile->getUsers()) { while ($user = $profile->fetch()) { echo $user->name; } } ?> I like it. So using a method similar to that, how would I create something like.. Where $db is the database connection. $query = $db->query("my query here"); $row = $query->fetchrow(); Using a method like yours, wouldn't work will it seeing as that one is specifically designed for that one query displayed in getUsers() And would using a method like.. $query = $db->query("my query here"); $row = $query->fetchrow(); be more secure/wise than simply doing.. $query = mysql_query("my query here"); $row = mysql_fetch_array($query); Quote Link to comment https://forums.phpfreaks.com/topic/197699-classes-and-functions-help/#findComment-1037572 Share on other sites More sharing options...
trq Posted April 6, 2010 Share Posted April 6, 2010 What do you mean "working on hat" thorpe? I meant 'that'. And would using a method like.. $query = $db->query("my query here"); $row = $query->fetchrow(); be more secure/wise than simply doing.. $query = mysql_query("my query here"); $row = mysql_fetch_array($query); Nope, nothing at all to do with security & if its not done properly, it would be of absolutely no benefit at all. Quote Link to comment https://forums.phpfreaks.com/topic/197699-classes-and-functions-help/#findComment-1037574 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.