0

So I have a problem I have an array that is passes to setData function after that I call getE that suppose to return the array but instead I'm getting Null what am I doing wrong?

<?php

class Se {      
    public $data1; 

    public function setData(array $data){
        if (empty($data)) {
        throw new InvalidArgumentException('The name of an employee cannot be empty.');
     }

    $data1 = $data;
        $data1 =  array_values($data1);
        var_dump($data1);   
    }


    public function getE(){ 
        return $data1[0];
    }
}

$tmpaaa= array('3333','222');
$ttt = new Se();
$ttt->setData($tmpaaa);

echo $ttt->getE();

So my revised code looks like this now

class Se {

    public $data1; 

public function setData(array $data)
{

if (empty($data)) 
{
throw new InvalidArgumentException('The name of an employee cannot be empty.');
 }
     $this->data1 = $data;     

}


    public function getE()
{   
return $this->$data1[0];

}



 };
$tmpaaa= array('3','2');
 $ttt = new Se();
$ttt->setData($tmpaaa);
echo $ttt->getE();
 ?>
6
  • Remove the echo $data1[0] line from getE. This doesn't appear in any of the answers and once again, you've not prefixed the class instance property with $this->. I strongly suggest you thoroughly read php.net/manual/language.oop5.properties.php Commented Oct 21, 2013 at 3:04
  • @Phil The new code it's updated above looks exactly the same as xlordt It doesn't run. but copying his example into new file works perfectly. I can't see the difference Commented Oct 21, 2013 at 3:18
  • You have $this->$data1[0]. It should be $this->data1[0]. Have you read the manual page I added above and in my answer? Commented Oct 21, 2013 at 3:24
  • Reading it right now @Phil Thanks for the help Now I understand. Sorry for the confusion Commented Oct 21, 2013 at 3:26
  • READING IT NOW?! You should have read it before you even started or at least 1 hour ago when first linked Commented Oct 21, 2013 at 3:27

2 Answers 2

3

In order to access class instance properties from within the class, you need to prefix the variable name with $this. See http://php.net/manual/language.oop5.properties.php

To fix your problem, change this in setData

$data1 = $data;
$data1 =  array_values($data1);
var_dump($data1);   

to this

$this->data1 = array_values($data);
var_dump($this->data1);

and getE to

public function getE(){ 
    return $this->data1[0];
}

Update

As it appears the $data1 property is required in Se, I'd set it in the constructor, eg

public function __construct(array $data) {
    $this->setData($data);
}

and instantiate it with

$ttt = new Se($tmpaaa);
echo $ttt->getE();
Sign up to request clarification or add additional context in comments.

9 Comments

Same thing still getting PHP Notice: Undefined variable: data1 in C:\php\ar_test.php on line 14 Notice: Undefined variable: data1 in C:\php\ar_test.php on line 14 NULL PHP Notice: Undefined variable: data1 in C:\php\ar_test.php on line 20 Notice: Undefined variable: data1 in C:\php\ar_test.php on line 20
@Harry_J I was just updating my answer to include the fix for getE as well
Now I'm getting PHP Notice: Undefined variable: data1 in C:\php\ar_test.php on line 14 Notice: Undefined variable: data1 in C:\php\ar_test.php on line 14 NULL PHP Notice: Undefined variable: data1 in C:\php\ar_test.php on line 20 Notice: Undefined variable: data1 in C:\php\ar_test.php on line 20 PHP Fatal error: Cannot access empty property in C:\php\ar_test.php on line 20 Fatal error: Cannot access empty property in C:\php\ar_test.php on line 20
@Harry_J What's on line 14 now? Still looks to me like you have $data1 instead of $this->data1
It was var_dump Now I'm getting array(2) { [0]=> string(4) "3333" [1]=> string(3) "222" } PHP Notice: Undefined variable: data1 in C:\php\ar_test.php on line 20 Notice: Undefined variable: data1 in C:\php\ar_test.php on line 20 PHP Fatal error: Cannot access empty property in C:\php\ar_test.php on line 20 Fatal error: Cannot access empty property in C:\php\ar_test.php on line 20
|
1

It is also recommended not closing the php tag in a class file, this prevents space issues.

<?php
class Se {      

    public $data1; 

    public function setData(array $data)
    {
        if (empty($data)) 
        {
            throw new InvalidArgumentException('The name of an employee cannot be empty.');
        }

        $this->data1 = array_values($data);  //you error was here, no need to to assign $data twice so I deleted top line.
    }


    public function getE()
    { 
        return $this->data1[0];

    }
}

$tmpaaa = array('3333','222');
$ttt = new Se();
$ttt->setData($tmpaaa);

echo $ttt->getE();

2 Comments

I edited the code replacing with $this->data1 and getting the errors I told @Phil in third comment
Just copy and paste any of these codes posted by me/phil, and compare.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.