hi all,

I was developing a blog where I used classes. but I met a problem when I try to include config.php to my classes and use global variables in the methodes. Normally without classes it works pretty much well.

here is the code

<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BlogComment
 *
 * @author Menuka
 */
include 'DBhandling.php';
include 'config.php';
//  $dob = new Dbhandling($dbHost,$dbUser,$dbName,$dbPass);

class BlogComment {

private $postId;

private $name_cm;
private $email_cm;
private $website_cm;
private  $comment_cm;

function __construct($inpostId=null, $inname_cm=null,$inemail_cm=nul,$inwebsite_cm=null,$incomment=null) {
  //initialization of properties.
    
  if(!empty ($inpostId)){
      $this->postId=$inpostId;//set postid and commentid toobject properties.
  }
  


  if(!empty ($inname_cm)){
  $this->name_cm=$inname_cm;
  }
  if(!empty ($inemail_cm)){
   $this->email_cm=$inemail_cm;
  }
  if(!empty ($inwebsite_cm)){
   $this->website_cm=$inwebsite_cm;
  }
  
  if(!empty ($incomment)){
      $this->comment_cm=$incomment;
  }
}

//this function is used to validate commentid
//check the postid and it's commentsid get the last commentid and increment then send it to database.
//$var, $name, $email, $website, $comment
function validateCommentIDandSave($inpostId, $inname_cm, $inemail_cm, $inwebsite_cm, $incomment){
 $dob = new DBhandling('localhost','root','classdb','needsome1');

  $dob->connectDB();

    if(!empty ($inpostId)){

        $qry="select MAX(commentId) from classdb.usercomments where postId='$inpostId'";
        $sql_qry = mysql_query($qry) or die(mysql_error());
           
        $row=mysql_fetch_array($sql_qry);
        $cm_id=$row[0];
      if(!$sql_qry){
         $cm_id=1;
       }else{
   
           $cm_id=$cm_id+1;

  
           
           $ob = new BlogComment();
           $ob->saveComment($inpostId, $cm_id, $inname_cm, $inemail_cm, $inwebsite_cm, $incomment);
            }
    
    
    } else{
        echo "invalide link. there is no post from this id";
    }
      $dob->closeDB();
}


 function saveComment($inpostId=null,$incommentId=null, $inname_cm=null,$inemail_cm=null,$inwebsite_cm=null,$incomment=null){

 $db=new DBhandling('localhost','root','classdb','needsome1');
 $db->connectDB();

$qury="insert into  classdb.usercomments(postid,commentId,name_cm,email_cm,website_cm,comment_cm,dateComment_cm,allow_cm) values ('$inpostId','$incommentId','$inname_cm','$inemail_cm','$inwebsite_cm','$incomment',CURRENT_TIMESTAMP , '0')";

  
    if (!mysql_query($qury))
  {
  die('Error: ' . mysql_error());
  }

header( 'Location: ../Blog/redirect.html' ) ;
echo "<br>";

     
  
//you can set another html page or java script show message.

$db->closeDB();
}

    //put your code here
}
?>

I try to replace the constructor with parameters in order to passe values from config.php to the constuctor then it doesn't wor
so I just give the real values directly. but it is not good programming to declare password and other stuff alway here and there
so how can I solve this

$dob = new DBhandling('localhost','root','classdb','needsome1');

here is code in the config.php

global $dbHost;
  global $dbUser;
  global $dbName;
  global $dbPass;
  
  $dbHost='localhost';
  $dbUser='root';
  $dbName='classdb';
  $dbPass='needsome1';

thx in advance...

Recommended Answers

All 4 Replies

Member Avatar for diafol

if you want to use globals inside classes, you have to decalre them as global inside the class (AFAIK). If you're using OOP, you probably need to avoid using globals as they break the 'rules'. I'd either go procedural or OOP. Creating a mongrel app will end with problems IMO.

hi,
yes when i declare it as global in the class we can accecess. but as it breaks the OOP concept I was avoided to do so. I declare it the password and other stuff as constant.
Do u think that also break the OOP concept? it works properly...

Member Avatar for diafol

It may work properly, but you need to maintain encapsulation (*I think* - OOP is not my strong point).

Perhaps you need to use DB abstraction - put it into its own class or you can use the very useful PDO.

In your config instead of declaring global you could try:

define(DB_HOST,'localhost');
define(DB_USER,'root');
define(DB_PASS,'needsome1');
define(DB_BASE,'classdb');

and then in your classes call

$dob = new DBhandling(DB_HOST,DB_USER,DB_BASE,DB_PASS);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.