-1

Possible Duplicate:
Reference - What does this symbol mean in PHP?

I am studying PHP MVC using this tutorial.There I found the following special notation used for object initialisation

$this->$model =& new $model;

I know & is used for representing ampersand(&) in HTML.What does it mean here?.The class in which the notation is found is given below.Also what about that tutorial?.Is it a good one?.If not can anybody refer me a better one?

<?php
class Controller {

    protected $_model;
    protected $_controller;
    protected $_action;
    protected $_template;

    function __construct($model, $controller, $action) {

        $this->_controller = $controller;
        $this->_action = $action;
        $this->_model = $model;

        $this->$model =&amp; new $model;
        $this->_template =&amp; new Template($controller,$action);

    }

    function set($name,$value) {
        $this->_template->set($name,$value);
    }

    function __destruct() {
            $this->_template->render();
    }

}

Community
  • 1
  • 1
Jinu Joseph Daniel
  • 4,856
  • 12
  • 53
  • 87
  • That doesn't look like valid PHP to me. Does it generate any errors or warning when you run it? – Dai Nov 24 '12 at 06:18
  • 5
    Please find a better tutorial. `$variable =& new Something` is a leftover from PHP 4, not needed in PHP 5. `mysql_*` functions are *deprecated* and should not be used in new code. Same goes for things like `register_globals` and `magic_quotes_gpc`. – DCoder Nov 24 '12 at 06:20
  • can you suggest me a good tutorial? – Jinu Joseph Daniel Nov 24 '12 at 06:23
  • Since you're following a tutorial to write your own MVC framework, I assume you have a basic foundational understanding of PHP. If my assumption isn't wrong, I would recommend you go through the source of an existing MVC framework to learn. Some good examples are `Fuel PHP` and `Slim PHP` – xbonez Nov 24 '12 at 06:25
  • 2
    STOP! That article is severely outdated! (Over 3 years ago!) [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained and the [deprecation process](http://j.mp/Rj2iVR) has begun on it. See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – Madara's Ghost Nov 24 '12 at 08:49

3 Answers3

3

& in this context is used for that object are assigned by reference, but its deprecated. Objects are assigned as reference by default in PHP 5, so no need to use this here.

Ravi
  • 2,068
  • 11
  • 23
2

&amp; translates to & so it would be assignment by reference. (by DCoders comment).
What you are seeing should actually be:

$this->$model =& new $model;

And =& in php relates to passing something by reference.

While some will argue that on PHP5 it's not needed (and I agree) it's not exactly the same thing and knowing it may be helpfull...

(from the manual)

One of the key-points of PHP 5 OOP that is often mentioned is that "objects are passed by references by default". This is not completely true.

This section rectifies that general thought using some examples.

Community
  • 1
  • 1
Frankie
  • 23,189
  • 10
  • 74
  • 112
  • Isn't passing by reference for a function, not an object? – Sterling Archer Nov 24 '12 at 06:20
  • 1
    @PRPGFerret, the manual has some explanations on that http://php.net/manual/en/language.oop5.references.php – Frankie Nov 24 '12 at 06:21
  • 2
    This is [*assignment by reference*](http://us.php.net/manual/en/language.references.whatdo.php#language.references.whatdo.assign), not *pass by reference*. – DCoder Nov 24 '12 at 06:22
  • Wouldn't creating a new isntance using the `new` keyword ensure that it is done by reference, anyways?. I understand the need to do this when doing `$a = & $b`, but why the need when using the `new` keyword? – xbonez Nov 24 '12 at 06:23
  • DCoder, gonna update the answer. Thnks! – Frankie Nov 24 '12 at 06:24
  • 1
    @xbonez: there's no need to do it in PHP 5, but it was necessary in PHP 4. – DCoder Nov 24 '12 at 06:26
  • @DCoder, there is a minor difference between the two... – Frankie Nov 24 '12 at 06:35
  • 1
    I might have phrased that incorrectly. There's no need to use assign by reference on a newly constructed object (`$a = new foo()` is okay), but there are obvious valid reasons to assign by reference in other contexts. In fact, `$a =& new foo()` in PHP 5 produces an error, either `E_STRICT` or `E_DEPRECATED` depending on the minor version. – DCoder Nov 24 '12 at 06:40
0

In PHP

$a = &$b

Means pass value by reference. In PHP from version 5 ALL objects are passed by reference by default. So you can just skip & symbol

$a = new b();
E_p
  • 3,088
  • 14
  • 28