Jump to content

Define multiple functions in one class


gansai

Recommended Posts

Segment.php

<?php
$mysqli = mysqli_connect("localhost","root","","squarellt"); 
$cid = required_param('id', PARAM_INT);
 class segment
 {   
   public function unitseg_set1()
   {          
	 $subject = $mysqli->query('SELECT * FROM order_subject WHERE id='.$cid.'');     
	 while($row=$subject->fetch_array() )
	 {
        echo '<div>'.$row['chem_name'].'</div>';
     }	 
   }
   
   public function unitseg_set2()
   {          
	 $subject = $mysqli->query('SELECT * FROM order_subject WHERE id='.$cid.'');     
	 while($row=$subject->fetch_array() )
	 {
        echo '<div>'.$row['physiotherapy_nn'].'</div>';
     }	 
   }   

   public function unitseg_set3()
   {          
	 $subject = $mysqli->query('SELECT * FROM order_subject WHERE id='.$cid.'');     
	 while($row=$subject->fetch_array() )
	 {
        echo '<div>'.$row['commun_gg'].'</div>';
     }	 
   }   
 }
 ?>

 home_segment.php
 
 <?php

   require_once('segment.php');

   $acc = new segment();
   $account1 = $acc->unitseg_set1();
   $account2 = $acc->unitseg_set2();
   $account3 = $acc->unitseg_set3();
   
   echo $account1;

   echo $account2;

   echo $account3;
?>

I was trying to pullout the content of all the functions defined from the class segment in segment.php. And calling all the three functions from a class segment in home_segment.php Is this a correct way of calling multiple functions from one class.

Or suggest with good OOP's concept. How could I improve the above similar functionality in Object Oriented Programming.

 

 

Link to comment
Share on other sites

You have (at least) two bits of extra reading to do

  1. Variable scope - your segment class is completely unaware of the $mysqli and $cid variables. (I would pass the $mysqli var as a constructor argument and pass the $cid in the calls to the methods.
  2. Using functions - your functions do not return anything, so $account1 etc will not contain anything. Make your functins return the values instead of echoing.
Link to comment
Share on other sites

No, that's really not the way to do it.

1. The code won't work. That's the biggest problem.
2. You're using a class that doesn't actually represent anything. It's simply a place you put a few functions. That is not OOP.
3. You're executing the exact same query multiple times. Doesn't that seem wasteful to you?
4. The functions that retrieve data also display it. If you wanted to display it differently then you would have to make even more functions. Having to repeat yourself like that is almost always a sign of larger a problem.
5. You act like the functions will return a value. They do not.

Here is the non-object oriented answer. Because you have to understand the simple concepts (PHP functions) before you can understand the complex concepts (OOP). And because making this part object-oriented is overkill; if you had much more code then it might make more sense to do it.

You have a query that needs to be executed. The data returned from the query can be used multiple ways, but the query itself is the same. Make that a function. Functions cannot use variables defined outside them, so you must pass what you need to it as arguments. And since this function will be used for multiple purposes, it must return the data - no HTML or other processing, just the data itself.

<?php

function unitseg($mysqli, $cid) {
	$subject = $mysqli->query('SELECT * FROM order_subject WHERE id = ' . (int)$cid);
	return $subject->fetch_all(MYSQLI_ASSOC);
}

?>

You call this function using the $mysqli and $cid that the other code had prepared.

<?php

require_once('segment.php');

$mysqli = mysqli_connect("localhost", "root", "", "squarellt"); 
$cid = required_param('id', PARAM_INT);

$accounts = unitseg($mysqli, $cid);

?>

It will return an array of rows from the results which you can foreach over. For example,

<?php

// ...

foreach ($accounts as $row) {
	echo '<div>', $row['chem_name'], '</div>';
}

?>

 

Link to comment
Share on other sites

Thank You for the response.

I'm looking for OOP concept, where I have a function with different queries. Since this is a public post, I am trying to give a sample query.

I need to execute one/more functions within a class and each function has to track records from database. Now I can call this class, a particular function in any file. 

Kindly do not use the keywords like "wasteful". I was trying to learn something technical. May be you're high developers, just motivate your skills to people. Many Thanks.

Link to comment
Share on other sites

25 minutes ago, Barand said:

You have (at least) two bits of extra reading to do

  1. Variable scope - your segment class is completely unaware of the $mysqli and $cid variables. (I would pass the $mysqli var as a constructor argument and pass the $cid in the calls to the methods.
  2. Using functions - your functions do not return anything, so $account1 etc will not contain anything. Make your functins return the values instead of echoing.

I agree segment class is unware of $mysqli. Could you suggest with code by taking a single function in a class. Many Thanks

Link to comment
Share on other sites

perhaps

<?php
class segment
{  
   private $dblink;
    
   public function __construct($dblink)
   {
       $this->dblink = $dblink;
   }
    
   public function get_chem_name($cid)
   {          
        $subject = $this->dblink->query('SELECT chem_name FROM order_subject WHERE id='.$cid.'');
        return $subject->fetch_all(MYSQLI_ASSOC);     
   }

   public function get_physio($cid)
   {          
       $subject = $this->dblink->query('SELECT physiotherapy_nn FROM order_subject WHERE id='.$cid.'');     
       return $subject->fetch_all(MYSQLI_ASSOC);    
   }   

   public function get_commun($cid)
   {          
       $subject = $this->dblink->query('SELECT commun_gg FROM order_subject WHERE id='.$cid.'');     
       return $subject->fetch_all(MYSQLI_ASSOC);     
   }   
}

/**
* output array as a series of divs
* 
* @param array $data
*/
function output_results($data)
{
    foreach ($data as $val) 
    {
        echo "<div>$val</div>";
    }
}

/**
*  get and output the data
*/

$mysqli = mysqli_connect("localhost","root","","squarellt"); 
$cid = required_param('id', PARAM_INT);


$acc = new segment($mysqli);

output_results ($acc->get_chem_name($cid));
output_results ($acc->get_physio($cid));
output_results ($acc->get_commun($cid));
     
?>

Don't use "select star", specify what columns you want. No need to slow down the query by transferring a shedload of data you don't need.

Use meaningful names - it makes life easier.

EDIT:

PS - I would also recommend you use prepared statements instead of placing variables directly into the sql string. And use PDO instead of mysqli - that too makes life much easier.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.