JonB Posted February 7, 2008 Share Posted February 7, 2008 This is going to sound terrible but I've been coding a PHP website for over a year but my understanding of some of the basics is, well, patchy (due to being self taught). I've managed to get by just fine without ever using -> or => but in trying to tweak a phpbb forum I'm coming across both a LOT. Could someone please explain what the two do and why I should be using them instead of other alternatives? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/89919-total-newbie-question-and/ Share on other sites More sharing options...
phpSensei Posted February 7, 2008 Share Posted February 7, 2008 -> Is used with Classes... $this-> var = "cool"; => Used in a array Its about as simple as i can explain to you. Quote Link to comment https://forums.phpfreaks.com/topic/89919-total-newbie-question-and/#findComment-460952 Share on other sites More sharing options...
The Little Guy Posted February 7, 2008 Share Posted February 7, 2008 => is comparison it means: - equal to or greater than It is almost like > so $i = 1; if($i => 1) returns true since $i is equal to one if($i > 1) returns false since $i is not greater than one ----------------------------------------------------- -> is for object orientated programming, so if you have ever programmed in java they use the period to go to methods within a class, well -> means the same thing. Quote Link to comment https://forums.phpfreaks.com/topic/89919-total-newbie-question-and/#findComment-460954 Share on other sites More sharing options...
phpSensei Posted February 7, 2008 Share Posted February 7, 2008 Also Here is an example of -> <?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(); ?> and you can read this also http://w3schools.com/php/php_operators.asp <= is less than or equal to 5<=8 returns true Quote Link to comment https://forums.phpfreaks.com/topic/89919-total-newbie-question-and/#findComment-460957 Share on other sites More sharing options...
Wolphie Posted February 7, 2008 Share Posted February 7, 2008 -> is used with classes, example below <?php class myclass() { function foo($name) { echo 'My name is ' . $name; } } $thisclass = new myclass(); $thisclass->foo('Wolphie'); ?> The ouput of the above would be "My name is Wolphie" => is used to assign keys in array, example below <?php $array = array("name" => "Wolphie", 2 => "Two"); echo $array['name']; echo '<br />'; echo $array[2]; ?> The above output would be: Wolphie 2 Quote Link to comment https://forums.phpfreaks.com/topic/89919-total-newbie-question-and/#findComment-460960 Share on other sites More sharing options...
JonB Posted February 7, 2008 Author Share Posted February 7, 2008 Cheers. With => I was thinking more along the lines of this code snippet >echo object_select_tag($comment, 'getPostId', array ('related_class' => 'Post',))</ With -> why would it be used in the following snippet? <?php $cart = new Cart; $cart->add_item("10", 1); $another_cart = new Cart; $another_cart->add_item("0815", 3); ?> Would $cart=add_item("10", 1); not work? Quote Link to comment https://forums.phpfreaks.com/topic/89919-total-newbie-question-and/#findComment-460962 Share on other sites More sharing options...
The Little Guy Posted February 7, 2008 Share Posted February 7, 2008 OOps, sorry my => is wrong, but if you make it >= then mine would be almost right, sorry. Quote Link to comment https://forums.phpfreaks.com/topic/89919-total-newbie-question-and/#findComment-460965 Share on other sites More sharing options...
phpSensei Posted February 7, 2008 Share Posted February 7, 2008 OOps, sorry my => is wrong, but if you make it >= then mine would be almost right, sorry. I was going to say something, but since your stats were like GURU, i knew i must have been wrong lol. Quote Link to comment https://forums.phpfreaks.com/topic/89919-total-newbie-question-and/#findComment-460968 Share on other sites More sharing options...
Wolphie Posted February 7, 2008 Share Posted February 7, 2008 Cart is a class And add_item is a function within that class So basically you're accessing the class Cart in order to use the function Quote Link to comment https://forums.phpfreaks.com/topic/89919-total-newbie-question-and/#findComment-460971 Share on other sites More sharing options...
JonB Posted February 7, 2008 Author Share Posted February 7, 2008 Ok, I understand the use of => in arrays now. Thanks guys! I'm still a little fuzzy on -> as I've never used classes in php. I'll investigate further myself now though as I do at least have a good starting point. Again, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/89919-total-newbie-question-and/#findComment-460972 Share on other sites More sharing options...
phpSensei Posted February 7, 2008 Share Posted February 7, 2008 Cheers. With => I was thinking more along the lines of this code snippet >echo object_select_tag($comment, 'getPostId', array ('related_class' => 'Post',))</ With -> why would it be used in the following snippet? <?php $cart = new Cart; $cart->add_item("10", 1); $another_cart = new Cart; $another_cart->add_item("0815", 3); ?> Would $cart=add_item("10", 1); not work? cart is a class, therefor when you make a variable like $cart, you want to access the class by doing " = new Cart"... Now when you want to access a VAR in the class, to print or manipulate the data you have to set the class by using the $cart, then you add a -> to point at the var in the class you want to change or print... You can also access functions using the -> Quote Link to comment https://forums.phpfreaks.com/topic/89919-total-newbie-question-and/#findComment-460973 Share on other sites More sharing options...
Wolphie Posted February 7, 2008 Share Posted February 7, 2008 Classes can be tricky if you haven't used them that often. Classes are usually used in OOP Quote Link to comment https://forums.phpfreaks.com/topic/89919-total-newbie-question-and/#findComment-460974 Share on other sites More sharing options...
phpSensei Posted February 7, 2008 Share Posted February 7, 2008 http://www.phpfreaks.com/tutorial_cat/19/Object-Oriented-Programming.php Refer to that link, to get a glimpse or idea of the power of OOP Quote Link to comment https://forums.phpfreaks.com/topic/89919-total-newbie-question-and/#findComment-460977 Share on other sites More sharing options...
revraz Posted February 7, 2008 Share Posted February 7, 2008 The FAQ is a great place http://www.phpfreaks.com/forums/index.php/topic,95867.0.html Quote Link to comment https://forums.phpfreaks.com/topic/89919-total-newbie-question-and/#findComment-460982 Share on other sites More sharing options...
The Little Guy Posted February 7, 2008 Share Posted February 7, 2008 Honestly PHP isn't meant to be a Object Orientated Programming Language. Java on the other hand is. In php the page runs through one... then it is done. If you refresh the go to a page the come back to the current page, the web site acts as if you are a brand new person (without using Sessions or Cookies or any other type of data transfer). Java and other OOP Languages on the other hand will remember you until the application shuts down. PHP shuts down every time the file completes its run through aka gets to the final line of code in a page. Basically OOP isn't meant for the web, the web is meant for procedural languages. OOP languages can be used on the web, but they do just as much as a procedural language does. Quote Link to comment https://forums.phpfreaks.com/topic/89919-total-newbie-question-and/#findComment-460984 Share on other sites More sharing options...
Wolphie Posted February 7, 2008 Share Posted February 7, 2008 I agree completely little guy. Quote Link to comment https://forums.phpfreaks.com/topic/89919-total-newbie-question-and/#findComment-460988 Share on other sites More sharing options...
JonB Posted February 7, 2008 Author Share Posted February 7, 2008 The FAQ is a great place http://www.phpfreaks.com/forums/index.php/topic,95867.0.html Sorry revraz. I did a search for -> and => but they're not exactly ideal search strings it seems I will try to refer to the FAQ before posting next time! Cheers for the OOP link. That pretty much spells it out to me, although I don't think there's any necessity for me to delve beyond just understanding at the moment. Final question on this (honestly), given that a few people seem to think OOP and web don't mix why do php frameworks and forums rely so heavily on them? Quote Link to comment https://forums.phpfreaks.com/topic/89919-total-newbie-question-and/#findComment-460990 Share on other sites More sharing options...
phpSensei Posted February 7, 2008 Share Posted February 7, 2008 OOP prevents re-writting the same script over and over again, and saves you alot of time. Even Custom Functions or Just PHP functions.... Another Great Link to Answer Your Question: http://www.sitepoint.com/article/php-paging-result-sets Quote Link to comment https://forums.phpfreaks.com/topic/89919-total-newbie-question-and/#findComment-460991 Share on other sites More sharing options...
Wolphie Posted February 7, 2008 Share Posted February 7, 2008 Then it's more of a library rather than OOP. Quote Link to comment https://forums.phpfreaks.com/topic/89919-total-newbie-question-and/#findComment-460993 Share on other sites More sharing options...
aschk Posted February 7, 2008 Share Posted February 7, 2008 OOP is becoming more apparent in web programming now, partly because it allows better structuring of your architecture and also because as demands on internet applications grow they're growing in complexity also, something which is harder to deal with with a procedural language. The real key to OOP is re-usability. Conceptually speaking, if you have a website (say amazon) each person whom connects to your site gets allocated a "cart" object (imagine shopping trolley in real life), in which they can put items (from the supermarket shelf). So turning it a bit technical each person can have a $cart = new Cart() object put into their session. And each time someone "adds" a product into their cart, the $cart->addProduct($id) function is called on that object. Basically it helps to keep it centralised in one re-usable container that can be altered in ONE PLACE ONLY to exhibit new functionality (or bug fixes) throughout your website application. Quote Link to comment https://forums.phpfreaks.com/topic/89919-total-newbie-question-and/#findComment-460999 Share on other sites More sharing options...
phpSensei Posted February 7, 2008 Share Posted February 7, 2008 Then it's more of a library rather than OOP. Oops, wrong link http://www.kirupa.com/developer/php/php_oop_intro_pg1.htm Quote Link to comment https://forums.phpfreaks.com/topic/89919-total-newbie-question-and/#findComment-461007 Share on other sites More sharing options...
aschk Posted February 7, 2008 Share Posted February 7, 2008 Futher info: There are a lot of issues to get around when moving from stateful (desktop applications) to stateless (http protocol/internet). And these are some of the things that frameworks are trying to overcome. PHP (as of version 5.0) began integrating OOP properly with the user of objects as references (don't worry if i've lost you), which from my point of view has revolutionised the way i've worked with online programming. No longer is everything on a page-by-page basis, and instead I can finally reuse inter-related components. I'm all for it personally. Quote Link to comment https://forums.phpfreaks.com/topic/89919-total-newbie-question-and/#findComment-461010 Share on other sites More sharing options...
The Little Guy Posted February 7, 2008 Share Posted February 7, 2008 or you can have a functions file with all your cart functions then do addProduct($id) and I can do the exact same thing as $cart->addProduct($id) and it is also just as clean I would then make ONE file called includes.php which then would contain all the functions files I need for any page. so it would then look like so: includes.php <?php session_start(); if($_SESSION['logged']){header('location: /')} // Check if user is logged in include 'shopingcart.php'; // Get shopping cart functions include 'math.php'; // Get math functions include 'strings.php'; // Get String functions include 'db.php'; // Database Connection ?> <?php include 'includes.php';?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <body> </body> </html> So... With those codes, why would I want to use classes? Quote Link to comment https://forums.phpfreaks.com/topic/89919-total-newbie-question-and/#findComment-461023 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.