我来自Java的新背景,现在正试图扩大我的视野。我对PHP有基本的了解,但我不是专家。我有一个基本的课程,如下所示
<?php
/**
* Controls and modifies the elements of the
*
* @author Tyler
*/
abstract class Element {
// Global variables
public $x_position = 0;
public $y_position = 0;
// First Constructor
public function Element () {
$x_position = 0;
$y_position = 0;
}
// Second Constructor
public function Element ($x, $y) {
$x_position = $x;
$y_position = $y;
}
}
我正在使用netbeans IDE为网站上的复杂元素开发此概念。但如果我不能理解这一点,我怎么能期望完成这项工作。我从IDE获得语法错误警告,我不熟悉PHP知道原因。 。 。然而。有什么建议吗?
*更新*
我做了一些研究后更新了我的代码。你会看看这个吗,看看我做得对吗
<?php
/**
* Controls and modifies the elements of the
*
* @author Tyler
*/
abstract class Element {
protected $x_position;
protected $y_position;
function _construct() {
}
// creates an instance of Elements without setting a position
public function withoutPosition () {
$instance = new self();
$this -> x_position = 0;
$this -> y_position = 0;
return $instance;
}
// creates an instance of Elements and sets a position
public function withPosition ($x, $y) {
$instance = new self();
$this -> x_position = $x;
$this -> y_position = $y;
return $instance;
}
}
最终更新
/**
* Basic class of web elements in web design app.
*
* @category element
* @copyright (c) 2015, Tyler Lazenby
* @author Tyler Lazenby
*/
abstract class Element {
/**
* horizontal position relative to left side of window.
* @var int
*/
private $x_pos;
/**
* vertical position relative to top of window.
* @var int
*/
private $y_pos;
/**
* vertical length of containing rectangle.
* @var double
*/
private $length;
/**
* horizontal width of containing rectangle.
* @var double
*/
private $width;
/**
* css hex code value of foreground color.
* @var string
*/
private $fg_color;
/**
* css hex code value of background color.
* @var string
*/
private $bg_color;
/**
* Default contructor.
*/
function _construct() {
}
/**
* creates an instance of Element with default values.
* @return \self
*/
public static function makeDefault() {
$instance = new self();
$this->x_pos = 0;
$this->y_pos = 0;
$this->length = 1;
$this->width = 1;
$this->fg_color = "#FFFFFF";
$this->bg_color = "#FFFFFF";
return $instance;
}
/**
* creates an instance of Element and sets a position.
* @param int $new_x
* @param int $new_y
* @return \self
*/
public static function makePosOnly($new_x, $new_y) {
$instance = new self();
$this->x_pos = $new_x;
$this->y_pos = $new_y;
return $instance;
}
/**
* creates an instance of Element and sets a size.
* @param double $new_length
* @param double $new_width
* @return \self
*/
public static function makeSizeOnly($new_length, $new_width) {
$instance = new self();
$this->length = $new_length;
$this->width = $new_width;
return $instance;
}
/**
* creates an instance of Element and sets a Fg color and Bg color.
* @param string $newFgColor
* @param string $newBgColor
* @return \self
*/
public static function makeColorOnly($newFgColor, $newBgColor) {
$instance = new self();
$this->fg_color = $newFgColor;
$this->bg_color = $newBgColor;
return $instance;
}
/**
* creates an instance of Element and sets a position and size.
* @param int $new_x
* @param int $new_y
* @param double $new_length
* @param double $new_width
* @return \self
*/
public static function makePosSize($new_x, $new_y, $new_length, $new_width) {
$instance = new self();
$this->x_pos = $new_x;
$this->y_pos = $new_y;
$this->length = $new_length;
$this->width = $new_width;
return $instance;
}
/**
* creates an instance of Element and sets a postion, Fg color, and Bg color.
* @param int $new_x
* @param int $new_y
* @param string $newFgColor
* @param string $newBgColor
* @return \self
*/
public static function makePosColor($new_x, $new_y, $newFgColor, $newBgColor) {
$instance = new self();
$this->x_pos = $new_x;
$this->y_pos = $new_y;
$this->fg_color = $newFgColor;
$this->bg_color = $newBgColor;
return $instance;
}
/**
* creates an instance of Element and sets a size, Fg color, and Bg color.
* @param double $new_length
* @param double $new_width
* @param string $newFgColor
* @param string $newBgColor
* @return \self
*/
public static function makeSizeColor($new_length, $new_width, $newFgColor, $newBgColor) {
$instance = new self();
$this->length = $new_length;
$this->width = $new_width;
$this->fg_color = $newFgColor;
$this->bg_color = $newBgColor;
return $instance;
}
/**
* creates an instance of Element and sets all attributes.
* @param int $new_x
* @param int $new_y
* @param double $new_length
* @param double $new_width
* @param string $newFgColor
* @param string $newBgColor
* @return \self
*/
public static function makeAll($new_x, $new_y, $new_length, $new_width, $newFgColor, $newBgColor) {
$instance = new self();
$this->x_pos = $new_x;
$this->y_pos = $new_y;
$this->length = $new_length;
$this->width = $new_width;
$this->fg_color = $newFgColor;
$this->bg_color = $newBgColor;
return $instance;
}
/**
* sets a new position.
* @param int $new_x
* @param int $new_y
*/
public function setPos($new_x, $new_y) {
$this->x_pos = $new_x;
$this->y_pos = $new_y;
}
/**
* returns an array of the current position.
* @return array
*/
public function getPos() {
return array($this->x_pos, $this->y_pos);
}
/**
* sets a new size.
* @param double $new_length
* @param double $new_width
*/
public function setSize($new_length, $new_width) {
$this->length = $new_length;
$this->width = $new_width;
}
/**
* returns an array of current size dimentions
* @return array
*/
public function getSize() {
return array($this->length, $this->width);
}
/**
* sets a new CSS hex color code for the foreground.
* @param string $new_FgColor
*/
public function setForeground($new_FgColor) {
$this->fg_color = $new_FgColor;
}
/**
* returns the current CSS hex color code for the foreground.
* @return string
*/
public function getForeground() {
return $this->fg_color;
}
/**
* sets a new CSS hex color code for the background.
* @param string $new_BgColor
*/
public function setBackground($new_BgColor) {
$this->bg_color = $new_BgColor;
}
/**
* returns the current CSS hex color code for the background.
* @return string
*/
public function getBackground() {
return $this->bg_color;
}
/**
* returns a string describing current state of all attributes.
* @return string
*/
function _toString() {
return "Position: (" . $this->x_pos . ", " . $this->y_pos . ")\n"
. "Size: " . $this->length . " by " . $this->width . "\n"
. "Color: Fore(" . $this->fg_color . ") Back(" . $this->bg_color . ")";
}
}
答案 0 :(得分:4)
要访问班级中的班级属性,您需要使用$this->x_position
而不是$x_position
。另外,您尝试定义的构造函数将是这样的,
public function __construct($x = 0, $y = 0) {
$this->x_position = $x;
$this->y_position = $y;
}
现在如果你调用默认构造函数,没有参数,它将使用默认值,如果你在调用它时提供参数,它将使用提供的值。