f



What is the difference between "=>" and "->" operators?

Morning,

I'm currently working my way through a series of tutorials from IBM's
developer network called "Cook up Web sites fast with CakePHP". The
XAMPP 1.4.9 development platform consists of PHP Version 5.0.2, MySQL
3.23.57.

Here is an excerpt of code provided in the tutorial, which is used to
create a model:

<?php
class Dealer extends AppModel
{
var $name = 'Dealer';
var $hasMany = array ('Product' => array(
'className' => 'Product',
'conditions'=>,
'order'=>,
'foreignKey'=>'dealer_id')
);
}
?>


Eclipse produces an error underneath the comma directly after the
'conditions' field, which says:
"Parse error: "Static/scalar constant expected.""

I changed the code above, got it working and continued through the
tutorial:

<?php
class Dealer extends AppModel
{
var $name = 'Dealer';
var $hasMany = array (
"Product" =>
array("className","Product","conditions","order","foreignKey","dealer_id"));
}
?>

The system works fine until I used Bake to implement an ACL. Each time
a new user registers, a rule is supposed to be enterred into the db
which defines what access level they have, but it doesn't work. When a
new user registers a new record should appear in the ACL tables. This
is the only thing that I can think might have caused a problem.

What is the difference between " => " and " -> " in PHP?

Cheers,

Michael

0
mejpark (19)
8/13/2007 10:27:19 AM
comp.lang.php 32646 articles. 0 followers. Post Follow

4 Replies
792 Views

Similar Articles

[PageSpeed] 58

mejpark wrote:
> Morning,

Good? :-x


> <?php
> class Dealer extends AppModel
> {
> var $name = 'Dealer';
> var $hasMany = array ('Product' => array(
> 'className' => 'Product',
> 'conditions'=>,
> 'order'=>,

you forget do define the value, assigned to array keys 'conditions' and 
'order'

> Eclipse produces an error underneath the comma directly after the
> 'conditions' field, which says:
> "Parse error: "Static/scalar constant expected.""

thats it, yes

> <?php
> class Dealer extends AppModel
> {
> var $name = 'Dealer';
> var $hasMany = array (
> "Product" =>
> array("className","Product","conditions","order","foreignKey","dealer_id"));

Its usually not the same.
You are using now a numeric indicated array and not the required 
associative array format.

You must read the documentation about arrays for understanding:
http://www.php.net/manual/en/language.types.array.php

> What is the difference between " => " and " -> " in PHP?

=> is only usable in array context. (see Array-Doc)

-> is to call some class sensitive Elements (e.g. Methods, Fields)
    access only non static Elements

for accessing static class elements use the double colon ::

So Long. Ulf
0
dr_logic (71)
8/13/2007 11:53:14 AM
mejpark wrote:

> What is the difference between " => " and " -> " in PHP?

'=>' is used when filling an array to provide an explicit key for a value.
e.g.:

	$team = array(
		'Bob',
		'Steve',
		'Dave',
		'manager'=>'Harry'
	);

is roughly equivalent to:

	$team = array();
	$team[0] = 'Bob';
	$team[1] = 'Steve';
	$team[2] = 'Dave';
	$team['manager'] = 'Harry';

'->' is an entirely different operator. It has nothing to do with arrays.
It it used to reference an object property or method in object-oriented
programming. It is equivalent to Java's "." operator. e.g.

	$myCup = new Cup($cupsize);
	echo $myCup->fill(LIQUIDS_ORANGE_JUICE, '300 mL');
	if ($myCup->percentageFilled==100)
	{
		echo "Cup is full.\n";
		if ($myCup->hasOverflowed)
			echo "Cup has overflowed!\n";
	}
	else
	{
		echo "There's still room for vodka!\n";
	}
	

-- 
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.12-12mdksmp, up 53 days, 15:46.]

PHP Debugging with Style -OR- How I Learned to Stop Worrying and Love the Bug
     http://tobyinkster.co.uk/blog/2007/08/12/php-debugging-with-style/
0
8/13/2007 12:15:03 PM
On Aug 13, 12:53 pm, Ulf Kadner <dr_lo...@gmx.net> wrote:
> mejpark wrote:
> > Morning,
>
> Good? :-x
>
> > <?php
> > class Dealer extends AppModel
> > {
> > var $name = 'Dealer';
> > var $hasMany = array ('Product' => array(
> > 'className' => 'Product',
> > 'conditions'=>,
> > 'order'=>,
>
> you forget do define the value, assigned to array keys 'conditions' and
> 'order'
>
> > Eclipse produces an error underneath the comma directly after the
> > 'conditions' field, which says:
> > "Parse error: "Static/scalar constant expected.""
>
> thats it, yes
>
> > <?php
> > class Dealer extends AppModel
> > {
> > var $name = 'Dealer';
> > var $hasMany = array (
> > "Product" =>
> > array("className","Product","conditions","order","foreignKey","dealer_id"));
>
> Its usually not the same.
> You are using now a numeric indicated array and not the required
> associative array format.
>
> You must read the documentation about arrays for understanding:http://www.php.net/manual/en/language.types.array.php
>
> > What is the difference between " => " and " -> " in PHP?
>
> => is only usable in array context. (see Array-Doc)
>
> -> is to call some class sensitive Elements (e.g. Methods, Fields)
>     access only non static Elements
>
> for accessing static class elements use the double colon ::
>
> So Long. Ulf

Thanks for that Ulf, very helpful response!

Until next time >>

0
mejpark (19)
8/13/2007 12:28:09 PM
On Aug 13, 1:15 pm, Toby A Inkster <usenet200...@tobyinkster.co.uk>
wrote:
> mejpark wrote:
> > What is thedifferencebetween " => " and " -> " in PHP?
>
> '=>' is used when filling an array to provide an explicit key for a value.
> e.g.:
>
>         $team = array(
>                 'Bob',
>                 'Steve',
>                 'Dave',
>                 'manager'=>'Harry'
>         );
>
> is roughly equivalent to:
>
>         $team = array();
>         $team[0] = 'Bob';
>         $team[1] = 'Steve';
>         $team[2] = 'Dave';
>         $team['manager'] = 'Harry';
>
> '->' is an entirely different operator. It has nothing to do with arrays.
> It it used to reference an object property or method in object-oriented
> programming. It is equivalent to Java's "." operator. e.g.
>
>         $myCup = new Cup($cupsize);
>         echo $myCup->fill(LIQUIDS_ORANGE_JUICE, '300 mL');
>         if ($myCup->percentageFilled==100)
>         {
>                 echo "Cup is full.\n";
>                 if ($myCup->hasOverflowed)
>                         echo "Cup has overflowed!\n";
>         }
>         else
>         {
>                 echo "There's still room for vodka!\n";
>         }
>
> --
> Toby A Inkster BSc (Hons) ARCS
> [Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
> [OS: Linux 2.6.12-12mdksmp, up 53 days, 15:46.]
>
> PHP Debugging with Style -OR- How I Learned to Stop Worrying and Love the Bug
>      http://tobyinkster.co.uk/blog/2007/08/12/php-debugging-with-style/
Haha, very amusing example!  Thankyou both for your explanations, that
has clarified thin gs in my mind!  Right, back to ACLs and the Star
Wars example then!

0
mejpark (19)
8/22/2007 11:21:14 PM
Reply: