开发者

What type of code that interprets correctly in procedural programming will cause an error when included in a class (outside of a function)?

开发者 https://www.devze.com 2023-04-07 15:16 出处:网络
I\'m new to oop and was surprised to see that code that worked properly in procedural programming, <?php

I'm new to oop and was surprised to see that code that worked properly in procedural programming,

<?php
$number_of_floors = 5;
$stairs_per_floor= 10;
echo $total_stairs= $number_of_floors*$stairs_per_floor;
?>

Lead to an error when included inside of a class:

<?php
// Class
class Building {
    // Object variables/properties
    public $number_of_floors = 5; // These buildings have 5 floors
    public $stairs_per_floor= 10;
    public $total_stairs= $number_of_floors*$stairs_per_floor;
    private $color;


    // Class constructor
    public function __construct($paint) {
        $this->color = $paint;
    }

    public function describe() {
        printf('This building has %d floors. It is %s in color.', 
            $this->number_of_floors, 
            $this->color
        );
    }
}

// Build a building and paint it red
$bldgA = new Building('red');

// Tell us how many floors these buildings have, and their painted color
$bldgA->describe();
?>

If you remove

public $total_stairs= $number_of_floors*$stairs_per_floor;

Everything works.

Are you not allowed to write arithmetic expressions inside of a class if they are outside of a function? What type of code that interprets correctly in procedural programming will ca开发者_开发百科use an error when included in a class (outside of a function)?


You can not do the operation at the time of defining them. Instead you should add this to your constructor and do:

$this->total_stairs = $this->number_of_floors * $this->stairs_per_floor;

Furthermore I advise you to use the generally accepted coding standards of PHP which would mean, not to use underscores in variable names.

public $totalStairs;
public $numberOfFloors;
public $stairsPerFloor;

Even more important is the choice of meaningful and readable variables names. So $bldgA should be:

$buildingA


you can't assign value by mathematical calculation while defining variable. Calculate value in constructor.

<?php
// Class
class Building {
    // Object variables/properties
    public $number_of_floors = 5; // These buildings have 5 floors
    public $stairs_per_floor= 10;
    public $total_stairs=0;
    private $color;


    // Class constructor
    public function __construct($paint) {
        $this->color = $paint;
        $this->total_stairs = $number_of_floors*$stairs_per_floor;
    }

    public function describe() {
        printf('This building has %d floors. It is %s in color.', 
            $this->number_of_floors, 
            $this->color
        );
    }
}

// Build a building and paint it red
$bldgA = new Building('red');

// Tell us how many floors these buildings have, and their painted color
$bldgA->describe();
?>


To answer your question: within an object oriented design, all code belongs inside a method; either a "special" method like the constructor, within a regular method, or (in languages other than PHP) in getter/setter methods (http://php.net/manual/en/language.oop5.properties.php has a way of implementing those in PHP).

Outside of methods, you're allowed to declare properties or attributes - but you should think of that really as a declaration, not a way of executing logic. The fact you can assign literals during the declaration is purely a convenience.


Don't assign expressions as variable. Do it in the Constructor:

$this->total_stairs = $this->number_of_floors * $this->stairs_per_floor;

or do

public $total_stairs= $this->number_of_floors * $this->stairs_per_floor;


you have to use the instance variables, without $this-> they are interpreted as local variables.

$this->total_stairs = $this->number_of_floors*$this->stairs_per_floor;

also, move those to the constructor, as they are (look like) instance specific.


You can't execute expressions when define properties, even with constants and heredocs.

Calculate them in __construct method.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号