[tssg-tech] Re: PHP help needed

  • From: Jim Cant <cant_jim@xxxxxxxxxxx>
  • To: <tssg-tech@xxxxxxxxxxxxx>
  • Date: Mon, 2 Jul 2012 10:10:18 -0400

Yeah, isn't having to use the 'this' keyword a bother.  Took me hours to solve 
Brian's problem when I got initiated into PHP.

I like the var_dump().

Here's a style suggestion; when echo-ing in php, always append a newline 
(distinct from a <br/> tag).  This will make the resulting HTML/JavaScript that 
goes to the browser much more readable.  You'll appreciate it when you get to 
debugging the HTML/JavaScript in Firebug (or your favorite browser debugging 
tool).

I've got a helper function I use to do just this:
     function echoln( $arg )
     {
            echo( $arg . "\n" );
     }
which I have in a file that I 'include_once' in php source.

Cheers,

Jim



Date: Mon, 2 Jul 2012 06:47:30 -0400
Subject: [tssg-tech] Re: PHP help needed
From: khaduch@xxxxxxxxx
To: tssg-tech@xxxxxxxxxxxxx

Hello -You probably have your solution - adding the "$this->" is the key, which 
Mary-Anne Wolf put in her code.
I am a PHP novice, but that is a standard OO construct that I didn't think of 
when looking at this initially.  There is one line in her code that doesn't 
work, it is a diagnostic output line in the "get" function: 
echo "value=$values[$key]"; 
As I was experimenting / learning with this, I came up with this code, and 
found the function 'var_dump' that was another interesting diagnostic tool.  My 
final code was this:

<?php
class myObj{  private $values = array();

  function __construct() {    $this->values['ONE']=1;    
$this->values['TWO']=2;    $this->values['THREE']=3;
    $this->values['FOUR']=4;    foreach ($this->values as $key => $value)      
echo $key.'=>'.$value.'<br />';
//var_dump($this->values);echo '<br>';echo 'var_dump of the newly constructed 
object within the constructor:<br>';
var_dump($this);echo '<br>';  }
    //Return key value if assigned, else return not assigned  public function 
get($key) {
    echo "myObj get fxn: key=$key".'<br>';    // var_dump ($this);
    if (isset($this->values[$key]))
        echo "<br>value=".$this->values[$key]."<br>";    if ( 
isset($this->values[$key])){
      return $this->values[$key];    }    return 'Not assigned!';  }}

echo 'Creating a new myObj at line 32<br>';$var = new myObj;echo "var_dump of 
the newly created object:<br>";var_dump($var); echo '<br>';
echo '<br><br>';echo 'The value is: '.$var->get('FOUR');
?>

Although the cut-and-paste wreaked a little havoc with it.  The output that I 
get in the browser when I run this:
Creating a new myObj at line 32
ONE=>1
TWO=>2
THREE=>3
FOUR=>4


var_dump of the newly constructed object within the constructor:
object(myObj)#1 (1) {
  ["values":"myObj":private]=>
  array(4) {
    ["ONE"]=>
    int(1)
    ["TWO"]=>
    int(2)
    ["THREE"]=>
    int(3)
    ["FOUR"]=>
    int(4)
  }
}

var_dump of the newly created object:
object(myObj)#1 (1) {
  ["values":"myObj":private]=>
  array(4) {
    ["ONE"]=>
    int(1)
    ["TWO"]=>
    int(2)
    ["THREE"]=>
    int(3)
    ["FOUR"]=>
    int(4)
  }
}



myObj get fxn: key=FOUR

value=4
The value is: 4
-- Ken

On Sun, Jul 1, 2012 at 10:31 AM, Play Cool Games <play@xxxxxxxxxxxxx> wrote:

Or upon further examination !

But not sure how to rewrite as I didn't understand the $key purpose in the 
original !





Warning

Reference of a $value and the last array element remain even after the foreach 
loop.

It is recommended to destroy it by unset().



Reference:

http://php.net/manual/en/control-structures.foreach.php



unset($value); // break the reference with the last element



On Jun 30, 2012, at 9:17 PM, Brian Marple wrote:



> What is wrong with this code?  It is as simple as all get out yet it doesn't 
> work!!!  Does anyone have any clues?

>

>

> <?php

>

> class myObj

> {

>  private $values = array();

>

>  public function __construct() {

>    $values['ONE']=1;

>    $values['TWO']=2;

>    $values['THREE']=3;

>

>    foreach ($values as $key => $value)

>      echo $key.'=>'.$value.'<br />';

>  }

>

>    //Return key value if assigned, else return not assigned

>  public function get($key) {

>    if (isset($values[$key])){

>      return $values[$key];

>    }

>    return 'Not assigned!';

>  }

>

> }

>

> $var = new myObj;

> echo 'The value is: '.$var->get('ONE');

>

> ?>

>

> --

> Brian Marple

> Systems Analyst

>

>

>

>



Lee T. Davy - Mini

Play Cool Games

play@xxxxxxxxxxxxx










                                          

Other related posts: