0

我成功创建了一个类并将其保存到页面中。我在这里使用了特殊的 php 字符。不使用那些无论如何都可以使用 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 =  '';
 } 
 ?>
4

1 回答 1

0

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

目的:

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

大批:

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

stdClass 的优势在于它是一个“纯”php 对象,默认情况下您可以通过 ref 将其发送到您的代码中,而无需在数组示例中设置 & 来通过 ref 传递它。

数组的优点是您可以将其设置在一行中。

于 2013-04-14T18:54:16.727 回答