2016-05-30 32 views
1

你好,大家好我有這個笨代碼的模式是:如何解決這個PHP錯誤「警告」?

<?php 

class Product extends CI_Model{ 
    var $title; 
    var $price; 
    var $name=" "; 
    public function __construct($title1,$price1,$name1) 
    { 
    parent::__construct(); 
    $this->load->database(); 
    $this->title=$title1; 
    $this->price=$price1; 
    $this->name=$name1; 
    } 
    function set_Name ($newname) 
    { 
    $this->name=$newname; 
    } 
    function get_Nmae() 
    { 
    return $this->name; 
    } 
    function set_title($newtitle){ 
    $this->title=$newtitle; 
    } 

    function set_price($newprice) { 
    $this->price=$newprice; 
} 
function get_productAttribute(){ 
    $x1=$this->name; 
    $x2=$this->title; 
    $x3=$this->price; 
    $productattribute = array("$x1","$x2","$x3"); 
    return $productattribute ; 

} 
public function get_data(){ 
    $query=$this->db->query('SELECT*FROM Saied'); 
    return $query->row_array(); 
} 
    public function insert_indb($var){ 
    if (is_a($var, 'Furniture')) 
    { 
$data= array(
    'id1' => '', 
    'name1'=> $this->title, 
    'title1' => $this->title, 
    'price1' => $this->price, 
    'size1' => $this->size, 
    'material1'=>$this->material 

    ); 
    $this->db->insert('Furn',$data); 
    return $data; 

} 
elseif (is_a($var, 'CDDVD')) 
{ 
    $data1= array(
    'id' => '', 
    'title'=>$this->title, 
    'price'=>$this->price, 
    'name'=>$this->name, 
    'sizemb'=>$this->sizeinmb."MB", 
    'manufacture'=>$this->manufacturer 
    ); 
    return $data1; 
} 
else 
{ 
    echo "no spicifed objech"; 
} 
} 
} 
class Furniture extends Product 
{ 
    var $size; 
    var $material; 
public function __construct($title1,$price1,$name1,$sizeset,$materialset) 
    { 
    $this->size=$sizeset; 
    $this->material=$materialset; 
    parent::__construct($title1,$price1,$name1); 
    $this->name1=" "; 
    } 
public function setAtribute_Furniture ($size1,$material1) 
    { 
    $this->size=$size1; 
    $this->material=$material1; 
    } 
} 
class CDDVD extends Product 
{ 
    var $sizeinmb; 
    var $manufacturer; 
    public function __construct($title1,$price1,$name1,$sizeinmbset,$manufacturerset) 
    { 
    $this->sizeinmb=$sizeinmbset; 
    $this->manufacturer=$manufacturerset; 
    parent::__construct($title1,$price1,$name1); 
    } 
    public function setAttribute_CDVD ($sizeinmb1,$manufacturer1) 
    { 
    $this->sizeinmb=$sizeinmb1."MB"; 
    $this->manufacturer=$manufacturer1; 
    } 
} 
$chair = new Furniture("ilove","200 euro"," ","200x200 ","wood"); 

print_r($chair->insert_indb($chair)); 

?> 

和負載控制器這種模式是:

public function __construct() 
     { 
     parent::__construct(); 
     $this->load->model('product'); 
     } 


     } 

事情是每一件事情的工作正是我想去的地方我插入intodatabase並打印每個類的陣列或數組1的值,但codigniter給我流動的錯誤/警告:遇到PHP錯誤

Severity: Warning 

Message: Missing argument 1 for Product::__construct(), called in /Applications/MAMP/htdocs/SandWeb/system/core/Loader.php on line 353 and defined 

Filename: models/Product.php 

Line Number: 7 

Backtrace: 

File: /Applications/MAMP/htdocs/SandWeb/application/models/Product.php 
Line: 7 
Function: _error_handler 

File: /Applications/MAMP/htdocs/SandWeb/application/controllers/Pages.php 
Line: 80 
Function: model 

File: /Applications/MAMP/htdocs/SandWeb/index.php 
Line: 315 
Function: require_once 

A PHP Error was encountered 

Severity: Warning 

Message: Missing argument 2 for Product::__construct(), called in /Applications/MAMP/htdocs/SandWeb/system/core/Loader.php on line 353 and defined 

Filename: models/Product.php 

Line Number: 7 

Backtrace: 

File: /Applications/MAMP/htdocs/SandWeb/application/models/Product.php 
Line: 7 
Function: _error_handler 

File: /Applications/MAMP/htdocs/SandWeb/application/controllers/Pages.php 
Line: 80 
Function: model 

File: /Applications/MAMP/htdocs/SandWeb/index.php 
Line: 315 
Function: require_once 

A PHP Error was encountered 

Severity: Warning 

Message: Missing argument 3 for Product::__construct(), called in /Applications/MAMP/htdocs/SandWeb/system/core/Loader.php on line 353 and defined 

Filename: models/Product.php 

Line Number: 7 

Backtrace: 

File: /Applications/MAMP/htdocs/SandWeb/application/models/Product.php 
Line: 7 
Function: _error_handler 

File: /Applications/MAMP/htdocs/SandWeb/application/controllers/Pages.php 
Line: 80 
Function: model 

File: /Applications/MAMP/htdocs/SandWeb/index.php 
Line: 315 
Function: require_once 

A PHP Error was encountered 

Severity: Notice 

Message: Undefined variable: title1 

Filename: models/Product.php 

Line Number: 11 

Backtrace: 

File: /Applications/MAMP/htdocs/SandWeb/application/models/Product.php 
Line: 11 
Function: _error_handler 

File: /Applications/MAMP/htdocs/SandWeb/application/controllers/Pages.php 
Line: 80 
Function: model 

File: /Applications/MAMP/htdocs/SandWeb/index.php 
Line: 315 
Function: require_once 

A PHP Error was encountered 

Severity: Notice 

Message: Undefined variable: price1 

Filename: models/Product.php 

Line Number: 12 

Backtrace: 

File: /Applications/MAMP/htdocs/SandWeb/application/models/Product.php 
Line: 12 
Function: _error_handler 

File: /Applications/MAMP/htdocs/SandWeb/application/controllers/Pages.php 
Line: 80 
Function: model 

File: /Applications/MAMP/htdocs/SandWeb/index.php 
Line: 315 
Function: require_once 

A PHP Error was encountered 

Severity: Notice 

Message: Undefined variable: name1 

Filename: models/Product.php 

Line Number: 13 

Backtrace: 

File: /Applications/MAMP/htdocs/SandWeb/application/models/Product.php 
Line: 13 
Function: _error_handler 

File: /Applications/MAMP/htdocs/SandWeb/application/controllers/Pages.php 
Line: 80 
Function: model 

File: /Applications/MAMP/htdocs/SandWeb/index.php 
Line: 315 
Function: require_once 

所以我的問題是我怎麼可以忽略這些警告或者我可能以錯誤的方式加載模型。 ?

+0

您可以發佈您創建產品的代碼嗎?您可以將var $ name改爲public $ name和public $ title和public $ price,而不是使用var $ name來查看是否刪除了一些警告 – Brett

+1

您正在以正確的方式加載模型,但在構造函數中需要3個值永遠不會發送,或者你退出它或初始化像'public function __construct($ title1 = NULL,$ price1 = NULL,$ name1 = NULL)'(這樣,你不能使用變量的一般值) – elddenmedio

+0

到最後我定義了一種產品:$ chair = new Furniture(「ilove」,「200 euro」,「」,「200x200」,「wood」); print_r($ chair-> insert_indb($ chair)); –

回答

0

您會收到此錯誤,因爲CI模型的構造函數無法接收參數。 CI的型號裝載機的 查看源代碼位於/YourCIAPP/system/Core/Loader.php名爲model

在這種方法中,你可以看到CI負荷模型與此代碼

require_once($mod_path.'models/'.$path.$model.'.php'); 
$this->_ci_models[] = $name; 
$CI->$name = new $model(); 

見,CI只是新模型對象與底部的方法Model類的構造函數沒有參數。

+0

好吧,所以,我應該使模型的第一類爲空構造函數,然後使其他類產品,傢俱...... ..,我想那麼它會工作,因爲我需要加載模型。在控制器中? ! –

+0

@saiedtrifess是的,有一個嘗試,我認爲它會工作。 –

+0

它沒有!有任何想法嗎 ? –

相關問題