Scope of $this in php

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 1670 Views - Last Post: 27 December 2012 - 10:26 AM Rate Topic: -----

#16 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,990
  • Joined: 08-June 10

Re: Scope of $this in php

Posted 27 December 2012 - 10:17 AM

View PostCTphpnwb, on 27 December 2012 - 05:02 PM, said:

Here's how I'd do it:

Honestly, I wouldn't. It has a public attribute with a setter, uses die() instead of proper error handling, and for some reason sets a <br> tag on the attribute in the constructor. I'd suggest something more along the lines of:
<?php
class Planet {
    private $world;

    public function __construct() {
        $this->setWorld("Hello world!");
    }

    public function getWorld() {
        return $this->world;
    }

    public function setWorld($val) {
        if(!empty($val)) {
            $this->world = $val;
        } 
        else {
            trigger_error("Earth must contain a string", E_USER_ERROR);
        }
    }
}

$newPlanet = new Planet();
echo $newPlanet->getWorld() . "<br>";
$newPlanet->setWorld($_GET["Planet"]);
echo $newPlanet->getWorld() . "<br>";



View PostDormilich, on 27 December 2012 - 05:12 PM, said:

Good thing that hardly anyone considers Java​Script a Functional Programming Language…

It's not, really. Javascript is far more OOP than functional. Only people who can't imagine a non class based language truly OOP think of it as functional. (And only because, when denying that it's OOP, it's the easiest alternative.) :)
Was This Post Helpful? 3
  • +
  • -

#17 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,236
  • Joined: 08-June 10

Re: Scope of $this in php

Posted 27 December 2012 - 10:18 AM

and to add to the confusion, one other way to do it:
class Planet {
    private $world;

    public function __construct() {
        $this->setWorld("Hello world!");
    }

    public function getWorld() {
        return $this->world;
    }

    public function setWorld($val) {
        if(empty($val)) {
            throw new Exception("World must contain a string.");
        } 
        $this->world = (string) $val;
    }
}

This post has been edited by Dormilich: 27 December 2012 - 10:19 AM

Was This Post Helpful? 3
  • +
  • -

#18 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2992
  • Posts: 10,337
  • Joined: 08-August 08

Re: Scope of $this in php

Posted 27 December 2012 - 10:22 AM

View PostAtli, on 27 December 2012 - 01:17 PM, said:

View PostCTphpnwb, on 27 December 2012 - 05:02 PM, said:

Here's how I'd do it:

Honestly, I wouldn't. It has a public attribute with a setter, uses die() instead of proper error handling, and for some reason sets a <br> tag on the attribute in the constructor.

True. I was trying to clean it up but keep the essence of it. The problem is that it's really all bad.
Was This Post Helpful? 0
  • +
  • -

#19 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,236
  • Joined: 08-June 10

Re: Scope of $this in php

Posted 27 December 2012 - 10:26 AM

View PostAtli, on 27 December 2012 - 06:17 PM, said:

View PostDormilich, on 27 December 2012 - 05:12 PM, said:

Good thing that hardly anyone considers Java​Script a Functional Programming Language…

It's not, really. Javascript is far more OOP than functional. Only people who can't imagine a non class based language truly OOP think of it as functional. (And only because, when denying that it's OOP, it's the easiest alternative.) :)/>

Even if it’s not a pure FL (like Scheme), it heavily borrows from there (Currying, First-Class Functions and Scheme as "ancestor"). Brendan Eich wanted to have FL functionality in Live​Script, after all.
Was This Post Helpful? 0
  • +
  • -

#20 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,990
  • Joined: 08-June 10

Re: Scope of $this in php

Posted 27 December 2012 - 10:26 AM

Dormilich said:

and to add to the confusion, one other way to do it:

Ahh, yes. Exceptions. They are probably the most appropriate way to deal with errors in an OOP context.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2