maxudaskin Posted July 28, 2008 Share Posted July 28, 2008 I still do not understand what a class does and why it should be used. Please explain it's use(s) and if needed, compare it to other PHP functions and features. Max Quote Link to comment Share on other sites More sharing options...
trq Posted July 28, 2008 Share Posted July 28, 2008 This ought get you started. Quote Link to comment Share on other sites More sharing options...
maxudaskin Posted July 28, 2008 Author Share Posted July 28, 2008 I still don't see the point. Why not just set the variables in the page? Quote Link to comment Share on other sites More sharing options...
trq Posted July 28, 2008 Share Posted July 28, 2008 Why not just set the variables in the page? Because it contaminates the global namespace. By placing methods and properties within a class you keep the global namespace clean. Quote Link to comment Share on other sites More sharing options...
maxudaskin Posted July 28, 2008 Author Share Posted July 28, 2008 I still don't get it. Can you give me a practical use? Quote Link to comment Share on other sites More sharing options...
trq Posted July 28, 2008 Share Posted July 28, 2008 Did you read the article I linked you too? Its a 3 part series as well, just take a look at the main sites tutorials section for the rest of them. Quote Link to comment Share on other sites More sharing options...
trq Posted July 28, 2008 Share Posted July 28, 2008 This is probably one of the best written explinations of some of the benifits Ive seen posted around here. If you still don't get it.. well.... Quote Link to comment Share on other sites More sharing options...
maxudaskin Posted July 28, 2008 Author Share Posted July 28, 2008 Did you read the article I linked you too? Its a 3 part series as well, just take a look at the main sites tutorials section for the rest of them. I did. So the only reason to have a class is to allow for the variable to be used again without worries? Quote Link to comment Share on other sites More sharing options...
trq Posted July 28, 2008 Share Posted July 28, 2008 Did you read the article I linked you too? Its a 3 part series as well, just take a look at the main sites tutorials section for the rest of them. I did. So the only reason to have a class is to allow for the variable to be used again without worries? I wouldn't say thats the only reason, but it is one of the benifits. Quote Link to comment Share on other sites More sharing options...
maxudaskin Posted July 28, 2008 Author Share Posted July 28, 2008 What other benefits are there? Quote Link to comment Share on other sites More sharing options...
maxudaskin Posted July 28, 2008 Author Share Posted July 28, 2008 In this PHP.NET code, does the A::Foo not work because you are calling foo without... umm -> allows for $this to be used while :: does not? Quote Link to comment Share on other sites More sharing options...
trq Posted July 28, 2008 Share Posted July 28, 2008 What other benefits are there? Read the articles, or do you really need one personally written here for you? In this PHP.NET code, does the A::Foo not work because you are calling foo without... umm -> allows for $this to be used while :: does not? As for that, I have no idea what you are talking about. Quote Link to comment Share on other sites More sharing options...
maxudaskin Posted July 28, 2008 Author Share Posted July 28, 2008 Sorry... didn't post the code... <?php class A { function foo() { if (isset($this)) { echo '$this is defined ('; echo get_class($this); echo ")\n"; } else { echo "\$this is not defined.\n"; } } } class B { function bar() { A::foo(); } } $a = new A(); $a->foo(); A::foo(); $b = new B(); $b->bar(); B::bar(); ?> Quote Link to comment Share on other sites More sharing options...
trq Posted July 28, 2008 Share Posted July 28, 2008 And a question? Quote Link to comment Share on other sites More sharing options...
maxudaskin Posted July 28, 2008 Author Share Posted July 28, 2008 Why would $A->Foo work but not A::Foo? Quote Link to comment Share on other sites More sharing options...
trq Posted July 28, 2008 Share Posted July 28, 2008 $this refers to the currently instantiated object, objects are only instantiated when you use the new keyword to create them, not when you attempt to call a class statically using :: Quote Link to comment Share on other sites More sharing options...
maxudaskin Posted July 28, 2008 Author Share Posted July 28, 2008 So basically, if you know C++, it is just like having a set of curly brackets, it is for namespace issues? Quote Link to comment Share on other sites More sharing options...
trq Posted July 28, 2008 Share Posted July 28, 2008 Classes make your code more independent and modular, and yes, help with namespace issues. Quote Link to comment Share on other sites More sharing options...
maxudaskin Posted July 28, 2008 Author Share Posted July 28, 2008 What are the other benefits other than that? Quote Link to comment Share on other sites More sharing options...
trq Posted July 28, 2008 Share Posted July 28, 2008 All covered in the articles I pointed you to. Quote Link to comment Share on other sites More sharing options...
maxudaskin Posted July 28, 2008 Author Share Posted July 28, 2008 I don't see any other benefit. That means (to me) that it is pointless... Quote Link to comment Share on other sites More sharing options...
gorgo666 Posted July 29, 2008 Share Posted July 29, 2008 This guy is obviously a wind up. I could write a book on benefits of object oriented programming, and I'm new to the idea, since only dabbling since September 2007. But it's late, and what's the point when this guy will find some way to shoot whatever I write, out the sky anyway. Hehe Quote Link to comment Share on other sites More sharing options...
maxudaskin Posted July 29, 2008 Author Share Posted July 29, 2008 I am just confused Quote Link to comment Share on other sites More sharing options...
gorgo666 Posted July 29, 2008 Share Posted July 29, 2008 It's natural to be confused, as programming is. But honestly, if you look at those articles, as provided by the moderator, they are pretty much the simplest explanation I have seen to OOP PHP. I admit yes, it is confusing, I get confused with inheritance, abstraction and bits like that, but the basic concept of classes is very, very simple. Just take a very simple example... You have a mobile phone, I presume. Imagine a class called MobilePhone. This can be "instantiated" by writing $myPhone = new MobilePhone() . You now have an object, assigned to $myPhone (a variable in PHP, I'm sure you'll know). You could now perform methods, which belong in that class, such as $myPhone->dial(123); or $myPhone->sendsms(...); and so on. Classes, I think, are just a way of organising things. I apologise if any syntax I've provided here is incorrect. This is a bit rushed. But seriously, those articles cover it all. Quote Link to comment Share on other sites More sharing options...
maxudaskin Posted July 29, 2008 Author Share Posted July 29, 2008 So lets say I have three functions in a class: login. $login = new login; When someone goes to the login page, it will check if logged in: if($login->checkLoggedin) checkLoggedin returns false How could I call a function in a class? if($login->checkLoggedin){ $login->displayForm; // Call the function... somehow } Quote Link to comment 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.