  | 
              
	              
	                
  
    
	 
        ส่งค่าตัวแปรไปอีกหน้ายังไง $test เขียน PHP แบบ OOP     | 
   
  
    |   | 
   
 
 
 
              
  
          
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ไม่ได้ลองนะ 
Code (PHP) 
<?php
define('host', 'localhost');
define('user', 'admin');
define('password', 'xxxxxx');
define('db', 'test');
class Load
{
	var $conn = NULL;
	function __construct($localhost='', $user='', $password='', $db="") {
		$this->conn = new mysqli($localhost, $user, $password, $db);
		$this->conn->query("SET names utf8");	
		if ($this->conn->connect_error) {
			$this->last_error = 'Cannot connect to database. ' . $conn->connect_error;
			exit;
		}
	}
	function query($str)
	{
		$result = $this->conn->query($str);
		return  $result->fetch_assoc();
	}
}
$obj = new load(host, user, password, db);
print_r($obj->query("SELECT * from customers"));
?>
                        
               
               | 
             
            
              
			                
  ประวัติการแก้ไข 2016-06-09 14:17:16 2016-06-09 14:17:43 2016-06-09 14:18:08              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2016-06-09 13:24:23 | 
                        By :
                            fossil31 | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 คือผมจะแยกส่วนนี้ไว้คนละหน้าอ่ะครับ 
 
Code (PHP) 
class load
{
	var $conn;
	function __construct() {
	{
		$this->conn = new mysqli('localhost', 'username', 'password', 'database');
		$this->conn->set_charset($db_charset);
		if ($this->conn->connect_errno) 
		{
			echo 'MySQL not connect';
			exit;
		}
	}
	public function query($strquery)
	{
		$result = $this->conn->query($strquery);
		return $result->fetch_assoc();
	}
}
 
 
เพิ่งเห็นที่แก้ เดี๋ยวผมลองศึกษาดูอีกทีครับเพิ่งเริ่มหัดเขียนแบบ OOP 
คือผมต้องการแบบนี้อ่าครับ 
 
FILE : connect.php 
Code (PHP) 
class load
{
	public function test
	{
		$test = 'test';
	}
}
 
 
FILE : index.php 
Code (PHP) 
require('Settings.php');
$obj = new load();
$obj -> test();
echo $test;
                        
               
               | 
             
            
              
			                
  ประวัติการแก้ไข 2016-06-09 15:33:00              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2016-06-09 15:21:11 | 
                        By :
                            nook00 | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 มันคือ รูปแบบ ครับ ให้เห็นการเชื่อมโยงระหว่าง ตัวแปร 
คุณก็ต้องไปประยุกต์ใช้ 
จะแยกแล้ว include ก็ไป refactor อีกทีครับ                        
               
               | 
             
            
              
			                
  ประวัติการแก้ไข 2016-06-09 15:27:22              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2016-06-09 15:26:37 | 
                        By :
                            fossil31 | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 อ่อได้แล้วครับเป็น 
 
file testcon.php 
Code (PHP) 
<?
class load
{
	public $test = '';
	
	public function test()
	{
	$this->test = 'text test';
	}
}
?>
 
 
file testtxt.php 
Code (PHP) 
<?
require_once('testcon.php');
$load=new load;
echo $load->test;
?>
                        
               
               | 
             
            
              
			                
  ประวัติการแก้ไข 2016-06-10 09:37:37              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2016-06-10 09:37:08 | 
                        By :
                            nook00 | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
      		  
	
     | 
   
 
                 |