Grazie mille per la risposta ho risolto il problema era il $ alla variabile del costruttore.
torson
@torson
Post creati da torson
-
RE: costruttore in php
-
costruttore in php
<html>
<head></head>
<body><?php
// Definizione della classe MyClass
class MyClass {
public $att1; // primo attributo
public $att2; // secondo attributo
// Costruttore
public function __construct($att1,$att2){
$this -> $att1 = $att1;
$this -> $att2 = $att2;}
// Permette di leggere $att1
public function getAtt1(){
return $this -> att1;
}
// Permette di leggere $att2
public function getAtt2(){
return $this->att2;
}
}
$nome="roberto";$cognome="pierro";
// Istanzio un oggetto di tipo MyClass
$obj = new MyClass($nome,$cognome);
echo "<br> stampo valore= " , $obj-> getAtt1()
?></body>
</html>il problema e che lquando chiamo il metodo
$obj-> getAtt1() è vuoto come faccio a leggere nei metodi i valori delle variabili del costruttore(se uso set e kiamo poi get funziona)ma con il costruttore grazie amici -
RE: problemi con funzioni in php
Ciao scusami per la domanda banale ma perchè si deve usare
$this->Nomevariabile anche nel metodo get non basta il nome della variabile???? E poi quando metto $this-> mi da un nuovo errore fatal error etcboh io pensavo di mettere le variabili private a global ma come si fa???:) Grazie mille.
-
RE: problemi con funzioni in php
Ciao grazie per la risposta ma proprio non riesco a trovare il problema :bho: se riesci ad essere più chiaro te ne sono grato ciao e grazie ancora.
-
problemi con funzioni in php
Cari amici ho il seguente problema:
[php]<html>
<head>
<title>Proviamo PHP</title>
<head><?php
class Employee {public function __construct ($name,$salary){
$this-> $name = $name;
$this-> $salary = $salary;
echo "<br>costruttore employee= " , $name , "<br>salary= " , $salary;
}public function getName(){
return $name;
}
public function getSalary(){
return $salary;
}public function calcolaSalary($val){
$num=100;
return ( $salary * $val)/$num ;
}
private $name;
private $salary;
}class Manager extends Employee {
public function __construct ($name,$salary){
$this-> $name = $name;
$this-> $salary = $salary;
parent:: __construct($name,$salary);// chiamo il costruttore di Employee
echo "<br>costruttore Manager= " , $name , "<br>salary=" , $salary;
$bonus=0;
}public function setBonus($b){
return $bonus=$b;
}public function getSalary(){
$baseSalary= parent:: getSalary();
return $baseSalary + $bonus ;
}private $bonus;
private $name;
private $salary;
}
?>
</head>
<body>
<?php$x=80; $y=50; $z=40; $Carl="carlo"; $luca="luca";
$boss = new Manager($Carl, $x);
$boss->setBonus($x);$staff0=$boss;
$staff1=new Employee ($luca, $y);
$staff2=new Employee ($luca, $z);$e= $staff0;
echo "<br><br>i valori sono = " , $boss->getName();
echo "<br><br>i salari sono = " , $boss->getSalary();$e= $staff1;
echo "<br><br>i valori sono = " , $e->getName();
echo "<br><br>i salari sono = " , $e->getSalary();?>
</body>
</HTML>[/php]Ma quando chiamo $e->getName() ;$e->getSalary();$boss->getName();
;$boss->getSalary(); mi stampa i salari sono =
Notice: Undefined variable: salary per tutti, come mai? Grazie mille per attenzione.