Jump to content

Total newbie question (-> and =>)


Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/89919-total-newbie-question-and/
Share on other sites

=> 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.

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 

-> 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

 

 

 

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?

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 ->

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.

 

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?

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.

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.

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.