سؤال

I am new to php and am trying to work with functions and classes. I am not sure why I cannot get the get and set methods to implement though. If you could take a look and let me know where my error is I'd appreciate it.

thanks so much:

<?php
class fileHandling
{
function __construct(){}

public $FName="red";
public function setFName($FName){$this->FName = $FName;}
public function getFName(){if($FName == null) return "";else return $FName;}

function toString(){return "file name: ".$this->getFName();}
}


$fh = new fileHandling();
echo($fh->toString()."<br>");
$fh->setFName("purple"."<br>");
echo("color is: ".$fh->Fname."<br>");
echo($fh->getFName());
$fh->setFName("orange"."<br>");
echo("color is: ".$fh->Fname."<br>");
echo($fh->getFName());
echo($fh->toString());
?>
هل كانت مفيدة؟

المحلول

public function getFName(){if($this->FName == null) return "";else return $this->FName;}

All thing's is good but you should replace $FName with $this->FName

نصائح أخرى

getFName is missing $this

public function getFName(){
  if($this->FName == null) return "";
  else return $this->FName;
}

There are a few problems here:

  • FName !== Fname: So $fh->Fname should be $fh->FName;
  • In your getter method you need $this->FName instead of $FName.
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top