Postingan lainnya
cara menggunakan function didalam function yg berasal dari file berbeda -PHP
halo agan2, caara make function di dlm function yg asal class dan file PHP nya beda itu gimana ya ??? udah baca beberapa artikel yg mirip2 tapi masih ga bisa ane mau manggil function yg isinya buat SELECT dari mysql, ane udh nyoba beberapa cara tapi gagal mulu, ane coba autoload, set di constructor sama bikin object class nya tapi masih error,(masih baru belajar gan maap kalo salah caranya)
ane butuh function nya buat dibikin file laporan ke pdf, kalo makenya ga didalem function ane berhasil tapi pas mau make di dalem function (file PrintAset function viewTable) gagal mulu
codingan nya dibawah gan
load.php di core folder
<?php
session_start();
spl_autoload_register(function($class){
require_once 'classes/' .$class. '.php';
});
$user = new User();
?>
User.php
<?php
class User{
private $_db;
public function __construct()
{
$this->_db = Database::getInstance();
}
public function get_asets()
{
return $this->_db->get_info('asets');
}
?>
nahh di file ini ane pengen make funtion nyaa dan selalu gagal :(
PrintAset.php
<?php
require_once 'core/load.php';
require_once 'report/fpdf.php';
class PrintAset extends FPDF
{
private $asets;
function __construct(){
$this->$asets = $user->get_asets();
}
function viewTable(){
$this->SetFont('Times','',8);
//$asets = $user->get_asets();
foreach ($asets as $aset) {
$this->MultiCell(25,10,$aset['data1'],1,'C');
$this->MultiCell(25,10,$aset['data2'],1,'C');
$this->MultiCell(25,10,$aset['data3'],1,'C');
$this->MultiCell(35,10,$aset['data4'],1,'C');
$this->MultiCell(35,10,$aset['data5'],1,'C');
$this->MultiCell(25,10,$aset['data6'],1,'C');
$this->MultiCell(35,10,$aset['data7'],1,'C');
}
}
}
// $user = new User();
$pdf = new PrintAset();
$pdf->AliasNbPages();
$pdf->AddPage('L','A4','0');
$pdf->headerTable();
$pdf->viewTable();
$pdf->Output();
?>
udh ane comment/uncomment, terus ga pake constructor tetep gabisa, hasil eror nya kaya gini gan
Notice: Undefined variable: user in C:\xampp\htdocs\_sikp\cetakAset.php on line 40
Fatal error: Uncaught Error: Call to a member function get_asets() on null in C:\xampp\htdocs\_sikp\cetakAset.php:40 Stack trace: #0 C:\xampp\htdocs\_sikp\cetakAset.php(57): CetakAset->viewTable() #1 {main} thrown in C:\xampp\htdocs\_sikp\cetakAset.php on line 40
bingung ane udh make autoload,bikin object usernya ko tetep undefined
2 Jawaban:
wkwwk maap gan, intinya ane cuma mau make function get_asets di file user, didalem function viewTable didalem file CetakAset
cuma kayanya cara ane manggil function nya salah