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 Link to comment https://forums.phpfreaks.com/topic/117058-php-classes/ Share on other sites More sharing options...
trq Posted July 28, 2008 Share Posted July 28, 2008 This ought get you started. Link to comment https://forums.phpfreaks.com/topic/117058-php-classes/#findComment-602055 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? Link to comment https://forums.phpfreaks.com/topic/117058-php-classes/#findComment-602060 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. Link to comment https://forums.phpfreaks.com/topic/117058-php-classes/#findComment-602064 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? Link to comment https://forums.phpfreaks.com/topic/117058-php-classes/#findComment-602071 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. Link to comment https://forums.phpfreaks.com/topic/117058-php-classes/#findComment-602072 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.... Link to comment https://forums.phpfreaks.com/topic/117058-php-classes/#findComment-602074 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? Link to comment https://forums.phpfreaks.com/topic/117058-php-classes/#findComment-602080 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. Link to comment https://forums.phpfreaks.com/topic/117058-php-classes/#findComment-602085 Share on other sites More sharing options...
maxudaskin Posted July 28, 2008 Author Share Posted July 28, 2008 What other benefits are there? Link to comment https://forums.phpfreaks.com/topic/117058-php-classes/#findComment-602086 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? Link to comment https://forums.phpfreaks.com/topic/117058-php-classes/#findComment-602091 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. Link to comment https://forums.phpfreaks.com/topic/117058-php-classes/#findComment-602093 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(); ?> Link to comment https://forums.phpfreaks.com/topic/117058-php-classes/#findComment-602094 Share on other sites More sharing options...
trq Posted July 28, 2008 Share Posted July 28, 2008 And a question? Link to comment https://forums.phpfreaks.com/topic/117058-php-classes/#findComment-602096 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? Link to comment https://forums.phpfreaks.com/topic/117058-php-classes/#findComment-602097 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 :: Link to comment https://forums.phpfreaks.com/topic/117058-php-classes/#findComment-602111 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? Link to comment https://forums.phpfreaks.com/topic/117058-php-classes/#findComment-602140 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. Link to comment https://forums.phpfreaks.com/topic/117058-php-classes/#findComment-602142 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? Link to comment https://forums.phpfreaks.com/topic/117058-php-classes/#findComment-602148 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. Link to comment https://forums.phpfreaks.com/topic/117058-php-classes/#findComment-602150 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... Link to comment https://forums.phpfreaks.com/topic/117058-php-classes/#findComment-602154 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 Link to comment https://forums.phpfreaks.com/topic/117058-php-classes/#findComment-602193 Share on other sites More sharing options...
maxudaskin Posted July 29, 2008 Author Share Posted July 29, 2008 I am just confused Link to comment https://forums.phpfreaks.com/topic/117058-php-classes/#findComment-602194 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. Link to comment https://forums.phpfreaks.com/topic/117058-php-classes/#findComment-602201 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 } Link to comment https://forums.phpfreaks.com/topic/117058-php-classes/#findComment-602205 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.