4

I had a quick look around at some of the similar questions to mine, but none of the answers quite touched on the problem I am having.

I have two files. product.php and index.php. Product.php defines the product class, and I'm just using index.php as a page to output the variables. Ideally, this would be used to output objects from different classes in the future.

product.php

class Product{
    public $var = "a default value";

    public function __construct($var){
        $var = $var;
    }

    public function displayVar(){
        echo $this->var;
    }
}

index.php (all enclosed in php tags obiously)

require_once("product.php");
$product = new Product();
echo $product->var;

I tried with different methods of including the file (require, include, include_once). I'm not sure if this is the issue.

The issue I'm getting is the $product->var doesn't output. Instead I get the following error:

Notice: Undefined property: Product::$var in C:\xampp\htdocs\FurnitureWebsite\index.php on line 21

I'm not entirely sure why it's considering this variable undefined. I looked through the PHP documentation on classes, and properties, but nothing that really looked like an answer.

A final note: I tried this with the class defined in index.php and it worked fine. This makes me think there might be something wrong with my file path, but the file name is spelt correctly, and the files are in the same directories, so I'm not totally sure.

2
  • $this->var = $var; change the value bind Commented Dec 21, 2016 at 11:03
  • a side note, there is no point in having a public getter displayVar to echo a public property. As it's public, you can access it directly. Or change the visibility of the property to protected or private if you want to restrict access to that property via an explicit getter. Commented Dec 21, 2016 at 11:05

6 Answers 6

1

Explainations

First of all, in your class, you specified a variable for you constructor. But, this variable is mandatory (meaning, there is no default value). So when instanciating a new Product, you have to give a value.

Also, if you're using a constructor, there is no need to give the variable a default value before the call to the constructor.

That said, if you want to access to your value without calling a method on it, there is plenty of solutions.

Solution 1: instanciation and direct call to the variable

class Product {

    public $var = 'a default value';

}

$product = new Product();

echo $product->var;

Solution 2: using an accessor to get the protected/private value

class Product {

    protected $var = 'a default value';

    public function getVar() {

        return $this->var;

    }

}

$product = new Product();

echo $product->getVar();

Solution 3: using the constructor to initialize the value

class Product {

    protected $var;

    public function __construct($var = 'a default value') {

        $this->var = $var;

        /* $this->var stands for protected $var */

        /* $var stands for... $var in the parameters */

    }

    public function getVar() {

        return $this->var;

    }

}

$product = new Product(); /* no value, so $var = 'a default value'; */

echo $product->getVar();

Solution 4: using a static variable

class Product {

    public static $var = 'a default value';

}

echo Product::$var;

Solution 5: using a static method with a protected static value

class Product {

    protected static $var = 'a default value';

    public static function getVar() {

        return self::$var;

    }

}

echo Product::getVar();

Documentation

PHP : classes and objects.

PHP : visibility.

PHP : static.

PHP : constructors and destructors.

Sign up to request clarification or add additional context in comments.

Comments

1
class Product{
    public $var = "a default value";

    public function __construct($var){
        $this->var = $var;
    }

    public function displayVar(){
        return $this->var;
    }
}

require_once("product.php");
$product = new Product('Product Name');
echo $product->displayVar();

Comments

1
class Product{
    protected $var;

    public function __construct($var = "a default value"){
        $this->var = $var;
    }

    public function getVar(){
        return $this->var;
    }
}

$product = new Product();
echo $product->getVar();
  • You have to give an value on new Product('your value'), your example has no value that is given to the constructor
  • Bind it right, not $var=$var do $this->var=$var
  • Set default value in __construct($var = "a default value"), better than directly at the property
  • Make the Variable protected not public else no getter is needed and the variable can changed from outside directly
  • create an getter for the variable, that returns the value, not echo it.

Thats it.

Comments

1

In your constructor you have this:

    public function __construct($var){
       $var = $var;
    }

Change it to:

    public function __construct($var){
       $this->var = $var;
    }

currently it just sets the passed variable to itself.

Second thing is that in your index.php file you create an object like this:

$product = new Product();

since your constructor expects some variable to get, you should always provide such. For example:

$product = new Product('new text');

I can see that you have a method to display a variable. That's good, although a common thing is to create such variable as protected and then creating a public getter method to access protected variables from outside the class. Then you can either simply call your method from index.php file like this:

$product->displayVar();

or create the getter method that will return your variable and display it in index.php file. eg.

/**product.php**/
public function getVariable() {
   return $this->var;
}


/**index.php**/
$product = new Product('new text');
echo $product->getVariable();

It depends on whether you will only need to display this variable or you know, that you will need this variable for other things. Either way, I think that creating the getter is better also because of a good habit.

I hope it helps. Good luck!

Comments

0

you have

public function __construct($var){
    $var = $var;
}

should be:

public function __construct($var){
    $this->var = $var;
}

Comments

0

Please modify your code like this

Product.php

<?php class Product{
    public $var ;
    public function __construct($var){
            $this->var = $var;
    } } ?>

index.php

<?php require_once 'product.php';
     $product = new Product('a default value');
     echo $product->var; ?>

It's may help you.

Thanks

Comments

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.