9
class A{
    public $name;

    public function __construct() {
      $this->name = 'first';
    }

    public function test1(){
        if(!empty($_POST["name"]))
        {
            $name = 'second';
        }
        echo $name;
    }

$f = new A;
$f->test1();

Why don't we get first and how set right default value variable $name only for class A?

I would be grateful for any help.

4
  • 4
    $this->name and $name are two different variables... Commented Jan 20, 2015 at 1:14
  • @deceze i know it, but how set in class default value for variable that print $name is first? if make public $name; $this->name='first'; it not will be print first. Commented Jan 20, 2015 at 1:19
  • 1
    I'm saying you need to use $this->name instead of $name inside your test1() function. The other part was fine as it was, you can revert that to your initial version. Commented Jan 20, 2015 at 1:28
  • @deceze it ok, i understood you and i understood how this work. Thanks! Commented Jan 20, 2015 at 1:37

3 Answers 3

17

You can use a constructor to set the initial values (or pretty much do anything for that matter) as you need to like this:

class example
{

    public $name;

    public function __construct()
    {
        $this->name="first";
    }

}

Then you can use these default values in your other functions.

class example
{

    public $name;

    public function __construct()
    {
        $this->name="first";
    }

    public function test1($inputName)
    {
        if(!empty($inputName))
        {
            $this->name=$inputName;
        }
        echo "The name is ".$this->name."\r\n";
    }

}

$ex=new example();
$ex->test1(" "); // prints first.
$ex->test1("Bobby"); // prints Bobby
$ex->test1($_POST["name"]); // works as you expected it to.
Sign up to request clarification or add additional context in comments.

Comments

6

you have two options to set the default values for the class attributes:

Option 1: set at the parameter level.

class A 
{
    public $name = "first";

    public function test1()
    {
        echo $this->name;
    }
}

$f = new A();
$f->test1();

Option 2: the magic method __construct() always being executed each time that you create a new instance.

class A 
{
    public $name;

    public function __construct() 
    {
        $this->name = 'first';
    }

    public function test1()
    {
        echo $this->name;
    }
}

$f = new A();
$f->test1();

1 Comment

Just wanted to know, that is there any difference between the two approaches? Is there any correct way to do it, or any best practice? Which one will you advise us to use? Thank you
-4

Use isset() to assign a default to a variable that may already have a value:

if (! isset($cars)) {
    $cars = $default_cars;
}

Use the ternary (a ? b : c) operator to give a new variable a (possibly default) value:

$cars = isset($_GET['cars']) ? $_GET['cars'] : $default_cars;

1 Comment

This does not answer the original question. Please have a look at the question again. Anyway, it would have been a good answer if the question was different. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.