Repeating a variable from a PHP class

I quickly looked through some of these questions, but none of the answers touched on the problem I am having.

I have two files. product.php and index.php . Product.php defines the class of the product, and I just use index.php as the page for outputting the variables. Ideally, this will 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)

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

I tried using various file inclusion methods (require, include, include_once). I am not sure if this is a problem.

The problem I get is $ product-> var, which is not output. Instead, I get the following error:

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

, undefined. PHP , .

: , index.php, . , - , , , .

+4
6

Explainations

, . ( , ). .

, , .

, , .

1:

class Product {

    public $var = 'a default value';

}

$product = new Product();

echo $product->var;

2: /

class Product {

    protected $var = 'a default value';

    public function getVar() {

        return $this->var;

    }

}

$product = new Product();

echo $product->getVar();

3:

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();

4:

class Product {

    public static $var = 'a default value';

}

echo Product::$var;

5:

class Product {

    protected static $var = 'a default value';

    public static function getVar() {

        return self::$var;

    }

}

echo Product::getVar();

PHP: .

PHP: .

PHP: .

PHP: .

+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();
+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();
  • new Product('your value'), ,
  • , $var=$var do $this->var=$var
  • __construct($var = "a default value"), ,
  • protected not public else getter,
  • getter , , echo.

.

+1

:

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

:

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

.

-, index.php :

$product = new Product();

, . :

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

, . , , protected, public getter . index.php :

$product->displayVar();

getter, index.php. .

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


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

, , . , , - .

, . !

+1

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

:

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

Please change your code as follows

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 can help you.

thank

0
source

Source: https://habr.com/ru/post/1664415/


All Articles