Czesc wszystkim!
Borykam sie z problemem juz od paru godzin.
Mianowicie musze skorzystac z niejawnego wywolania metody __toString() w moim obiekcie, aby ten zwrocil mi symbol Newtona.
PHP zwraca mi blad:
Catchable fatal error: Object of class Newton could not be converted to string in /var/www/html/Zadanie_2.php on line 167
Nie mam juz pojecia jak to zrobic, moze ktos cos doradzi?
Nie wiem tez jak wstawic te znaczniki MathML do PHP, szukalem i szukalem tego i ciagle wszystko jest w html/xml.
Gdyby ktos mogl mnie naprowadzic na rozwiazanie problemu bylbym bardzo wdzieczny!
Tak brzmi zadanie:
Uzupelnij definicje klasy Newton o metody, ktorePHP wykorzysta niejawnie podczas konwersji do typu string, ktora pozwoli na przedstawienie symbolu
Newtona w postaci odpowiedniego zestawu znacznikow jezyka MathML.Przyklad symbolu Newtona wygenerowanego w jezyku MathML znajdziesz w pliku MathML.html
Plik MathML.html
CODE
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<p> This is a Newton symbol:
</p>
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow>
<mo>(</mo>
<mfrac linethickness="0">
<mi>n</mi>
<mrow>
<mi>k</mi>
</mrow>
</mfrac>
<mo>)</mo>
</mrow>
</math>
Plik z silnia:
CODE
<?php
define("E_NO_INT",0xff000001
); define("E_NEGATIVE",0xff000002
); define("E_TOO_BIG",0xff000003
); define("E_INVALID_CONST",0xff000004
);
class Silnia
{
protected $base;
protected
static $object_count = 0; protected $liczba_arg;
protected $argument;
public function __construct()
{
self::$object_count++;
if($this->liczba_arg == 0)
{
$this->base = 0;
if(self::$object_count == 1)
self::$silniaTab = array(1
,1
,2
,6
,24
,120
,720
,5040
,40320
,362880
,3628800
,39916800
,479001600
,6227020800
,87178291200,1307674368000,20922789888000);
}else if($this->liczba_arg == 1)
{
if (is_integer($this->argument) && $this->argument >= 0
&& $this->argument <= 16
) { $this->base = $this->argument;
if(self::$object_count == 1)
self::$silniaTab = array(1
,1
,2
,6
,24
,120
,720
,5040
,40320
,362880
,3628800
,39916800
,479001600
,6227020800
,87178291200,1307674368000,20922789888000);
}else if (is_float($this->argument) && $this->argument >= 0
&& $this->argument <= 16
) { return E_NO_INT;
}else if (is_numeric($this->argument) && $this->argument < 0
) { return E_NEGATIVE;
}else if(is_numeric($this->argument) && $this->argument > 16
){ return E_TOO_BIG;
}
}else {
return E_INVALID_CONST;
}
}
function Calculate()
{
if($this->liczba_arg == 0)
{
return self::$silniaTab[$this->base];
}else if($this->liczba_arg == 1)
{
if (is_integer($this->argument) && $this->argument >= 0
&& $this->argument <= 16
) {
return self::$silniaTab[$this->base];
}else if (is_float($this->argument) && $this->argument >= 0
&& $this->argument <= 16
) { return E_NO_INT;
}else if (is_numeric($this->argument) && $this->argument < 0
) { return E_NEGATIVE;
}else if(is_numeric($this->argument) && $this->argument > 16
){ return E_TOO_BIG;
}
}else if($this->liczba_arg > 1){
return E_INVALID_CONST;
}
}
function __toString()
{
return $this->base . '! = ' . $this->Calculate();
}
}
?>
Plik z symbolem Newtona:
CODE
<?php
include_once('Silnia.inc');
define("E_INVALID_N_CONST",0xff000005
); class Newton extends Silnia {
protected $n,$k;
protected
static $object_count = 0;
function __construct(){
if (method_exists($this,$f='__construct'.$i)) {
call_user_func_array
(array($this,$f),$a); }
else
{
throw new Exception("Undefined constructor for given set of arguments",E_INVALID_N_CONST);
}
}
function __construct2($in,$ik){
//$this->liczba_arg = 1;
//$this->argument = func_get_args();
//self::$silniaTab = array(1,1,2,6,24,120,720,5040,40320,362880,3628800,39916800,479001600,6227020800
,87178291200,1307674368000,20922789888000);
$this->n=$in;
$this->k=$ik;
//echo self::$silniaTab[5].' '.$this->n.' '.$this->k;
//$this->Calculate();
}
function Calculate(){
//$tmp = $this->argument[1];
//$this->argument = $this->argument[0];
parent::__construct();
$this->base = $this->n;
//echo "base = ".$this->base."<br>";
//$this->argument = $this->n;
//echo "arg = ".$this->argument."<br>";
//echo self::$silniaTab[5]."<br>";
$l1 = parent::Calculate();
//echo "l1 = ".$l1."<br>";
$this->base = $this->k;
//echo "base = ".$this->base."<br>";
//$this->argument = $this->k;
//echo "arg = ".$this->argument."<br>";
$m1 = parent::Calculate();
//echo "m1 = ".$m1."<br>";
$this->base = ($this->n - $this->k);
//echo "base = ".$this->base."<br>";
//$this->argument = $this->base;
//echo "arg = ".$this->argument."<br>";
$m2 = parent::Calculate();
//echo "m2 = ".$m2."<br>";
return $l1/($m1*$m2);
}
function __set($name,$value)
{
if ($name == 'n') {
$this->setN($value);
}else if ($name == 'k') {
$this->setK($value);
}
}
function __get($name)
{
if ($name == 'n') {
return $this->getN();
}else if ($name == 'k') {
return $this->getK();
}
}
function getN ()
{
return $this->n;
}
function getK ()
{
return $this->k;
}
function setN ($in)
{
if(!is_int($in)) throw
new Exception
("no integer value",E_NO_INT
); if($in<0) throw new Exception("negative value",E_NEGATIVE);
if($in>16) throw new Exception("value too big",E_TOO_BIG);
$this->n=$in;
}
function setK ($ik)
{
if(!is_int($ik)) throw
new Exception
("no integer value",E_NO_INT
); if($ik<0) throw new Exception("negative value",E_NEGATIVE);
if($ik>16) throw new Exception("value too big",E_TOO_BIG);
$this->k=$ik;
}
}
function __toString()
{
return "nie mam pojecia co tu wstawic";
}
$n1=new Newton(5,3);
echo "wynik: " . $n1->Calculate() . "<br>";
echo "nasze n to " . $n1->n . "<br>"; echo "nasze k to " . $n1->k . "<br>"; echo "zmieniam n na 7<br> "; $n1->n=7;
echo "zmieniam k na 4<br> "; $n1->k=4;
echo "nowa warto��: " . $n1->Calculate() . "<br>";
echo "Symbol newtona zapisuje si� tak: $n1 <br>"; echo "A tak dzia�a MathML $n1";
?>
Ten post edytował Amasend 19.02.2016, 20:22:38