创建一个具有数组属性的类更好的方法

时间:2013-04-14 18:05:46

标签: php arrays oop

我已成功创建一个类并将其保存到页面。我在这里使用了特殊的php charackets。无论如何使用它们无论如何都要在OOP中使用任何魔术方法创建一个类

请看这个波纹管孔代码。我全面更新

<?php

$title = null;
$online = null;
$offline = null;
$email = null ;
$metadescription = null;
$metakeywords = null ;
$copyright = null;
$pard_host = null;
$pard_database = null;
$username = null;
$password = null ;

if(isset($_POST["title"])){$title = $_POST["title"];}
if(isset($_POST["online"])){$online = $_POST["online"];}
if(isset($_POST["offline"])){$offline = $_POST["offline"];}
if(isset($_POST["email"])){$email = $_POST["email"];}
if(isset($_POST["meta-description"])){$metadescription = $_POST["meta-description"];}
if(isset($_POST["meta-keywords"])){$metakeywords = $_POST["meta-keywords"];}
if(isset($_POST["copyright"])){$copyright = $_POST["copyright"];}
if(isset($_POST["pard_host"])){$pard_host = $_POST["pard_host"];}
if(isset($_POST["pard_database"])){$pard_database = $_POST["pard_database"];}
if(isset($_POST["username"])){$username = $_POST["username"];}
if(isset($_POST["password"])){$password = $_POST["password"];}  



class pard_admin_Config{


    function __construct($TITLE,$ONLINE,$OFFLINE,$EMAIL,$METADESCRIPTION,$METAKEYWORDS,$COPYRIGHT,$HOST,$DATABASE,$USERNAME,$PASSWORD) {
        $this->TITLE = $TITLE;
        $this->ONLINE= $ONLINE;
        $this->OFFLINE = $OFFLINE;
        $this->EMAIL = $EMAIL;
        $this->METADESCRIPTION = $METADESCRIPTION;
        $this->METAKEYWORDS= $METAKEYWORDS;
        $this->COPYRIGHT = $COPYRIGHT;
        $this->HOST = $HOST;
        $this->DATABASE= $DATABASE;
        $this->USERNAME = $USERNAME;
        $this->PASSWORD = $PASSWORD;
    }
}

$adminConfig  = new pard_admin_Config($title,$online,$offline,$email,$metadescription,
$metakeywords,$copyright,$pard_host,$pard_database,$username,$password);

$pardConfig = new PDO('mysql:host=localhost;dbname=pard', "root", "");
if(isset($pardConfig)){
    echo $adminConfig->TITLE;

    }
$SQL =<<<'EOD'
CREATE TABLE IF NOT EXISTS pard_admin_config(
title varchar(255) NOT NULL,
online varchar(100),
offline varchar(100),
email varchar(100),
metades varchar(200),
metakey varchar(200),
copyright varchar(200),
pard_host varchar(100),
pard_database varchar(100),
username varchar(100),
password varchar(100)
)
EOD;

$pardConfig->query($SQL);


$stmt = $pardConfig->prepare("INSERT INTO pard_admin_config (title, online, offline, email, metades, metakey, copyright, pard_host, pard_database, username, password) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE 
title=values(title), online=values(online), offline=values(offline), email=values(email), metades=values(metades), metakey=values(metakey), copyright=values(copyright), pard_host=values(pard_host), pard_database=values(pard_database), username=values(username), password=values(password)");
 $ConfigData = array($adminConfig->TITLE,
 $adminConfig->ONLINE,
 $adminConfig->OFFLINE,
 $adminConfig->EMAIL,
 $adminConfig->METADESCRIPTION,
 $adminConfig->METAKEYWORDS,
 $adminConfig->COPYRIGHT,
 $adminConfig->HOST,
 $adminConfig->DATABASE,
 $adminConfig->USERNAME,
 $adminConfig->PASSWORD);
$stmt->execute($ConfigData);


$classProperties =  "<?php \n class PardFactory { \n public \$TITLE = '$adminConfig->TITLE';\n public \$ONLINE = '$adminConfig->ONLINE';\n public \$OFFLINE =  '$adminConfig->OFFLINE';\n public \$EMAIL =  '$adminConfig->EMAIL';\n public \$METADESCRIPTION =   '$adminConfig->METADESCRIPTION';\n public \$METAKEYWORDS =  '$adminConfig->METAKEYWORDS';\n public \$COPYRIGHT =  '$adminConfig->COPYRIGHT';\n public \$HOST =  '$adminConfig->HOST';\n public \$DATABASE =  '$adminConfig->DATABASE';\n public \$USERNAME =  '$adminConfig->USERNAME';\n public \$PASSWORD =  '$adminConfig->PASSWORD';\n } \n ?>";

$file = fopen("../../pard_config/configuration.php","w");
fwrite($file,$classProperties);
fclose($file);
?> 

此代码创建一个类

<?php 
 class PardFactory 
 { 
     public $TITLE = '';
     public $ONLINE = '';
     public $OFFLINE =  '';
     public $EMAIL =  '';
     public $METADESCRIPTION =   '';
     public $METAKEYWORDS =  '';
     public $COPYRIGHT =  '';
     public $HOST =  '';
     public $DATABASE =  '';
     public $USERNAME =  '';
     public $PASSWORD =  '';
 } 
 ?>

1 个答案:

答案 0 :(得分:0)

如果您只需要一个表示数据的简单对象,则可以使用stdClass或关联数组:

对象:

$cls = new stdClass();
$cls->param = 'string';
$cls->{'param2'} = 12;

数组:

$arr = array(
    'param'=> 'string',
    'param2' => 12
);

stdClass的优点是它是一个“纯粹的”php对象,你可以在默认情况下通过ref发送它代码而不设置&amp;在数组示例中通过ref传递它。

数组的优点是你可以在一行中设置它。