Jump to content
BrainPill

how to use a second function inside a class?

Recommended Posts

This question is based on the table and class set up in my former posting.

The difference is that I want to have a second function inside the class. 

The name of this second function is: check_amount () . 

I do not see any output or action at all in this function. 

I hope someone can help to make the example work.

 

 

Database table: 

-- phpMyAdmin SQL Dump
-- version 4.7.4
-- https://www.phpmyadmin.net/
--
-- Host: localhost:3306
-- Generation Time: Oct 04, 2018 at 08:42 AM
-- Server version: 5.7.19
-- PHP Version: 7.1.9

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `test_fruit`
--

-- --------------------------------------------------------

--
-- Table structure for table `fruit`
--

DROP TABLE IF EXISTS `fruit`;
CREATE TABLE IF NOT EXISTS `fruit` (
  `rec_nr` int(2) NOT NULL AUTO_INCREMENT,
  `fruit_type` varchar(50) DEFAULT NULL,
  `amount` int(3) NOT NULL,
  `created_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `changed_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`rec_nr`,`created_on`,`changed_on`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;

--
-- Dumping data for table `fruit`
--

INSERT INTO `fruit` (`rec_nr`, `fruit_type`, `amount`, `created_on`, `changed_on`) VALUES
(1, 'apple', 5, '2018-10-04 06:01:48', '2018-10-04 06:01:48'),
(2, 'strawberry', 20, '2018-10-04 06:01:48', '2018-10-04 06:01:48'),
(2, 'banana', 15, '2018-10-04 06:02:54', '2018-10-04 06:02:54'),
(4, 'grapes', 9, '2018-10-04 06:02:54', '2018-10-04 06:02:54');
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

 

Script 1:

 

		<?php 

		$amount = '' ; // 5, 20 , 15, 9 
		$fruit_type = 'grapes'; // grapes , apple, strawberry, banana
		$dbname = "test_fruit";


		include 'fruit-class4a.php';
		$object = new fruit_stock( '$fruit_type','$dbname'); 
		 
		$object -> get_fruit($amount, $fruit_type, $dbname);
		 


		// $object ->get_fruit($amount, $fruit_type, $dbname)->check_amount($amount);
		 

		var_dump($object);

		echo $object -> get_fruit($amount, $fruit_type, $dbname);


		?>

 

Script 2:

 

	<?php 
			
			class fruit_stock {
			 
				var $amount;
				var $dbname;
				
			public function get_fruit($amount, $fruit_type, $dbname) {
				  
			 global $amount; 
			var_dump($fruit_type);
			var_dump($dbname);
		  
		
			include 'login-test.php'; // login credentials
	 
			$conn = new mysqli($servername, $username, $password, $dbname);
	 
			if ($conn->connect_error) {
			die("Connection failed: " . $conn->connect_error);
			}

			// PREPARED 

		if ($stmt = $conn->prepare("SELECT amount FROM fruit WHERE fruit_type = ? ; " )) { 
	 

	 
		$stmt->bind_param('s', $fruit_type); //  
	 
		$stmt->execute();
	   
		$stmt->bind_result($amount);
	 
		 
		 
		 
		while ($stmt->fetch()) {
			
		   var_dump($amount);
		  
			   return $amount;	 
			 
		} 
	}
	 
	} 
			 
		
			public function check_amount($amount) {
			 
			  $amount = $amount + 1;
			 var_dump($amount);
			 return $amount; } } ?>

 

Share this post


Link to post
Share on other sites
class whatever {



function func1 {

do stuff
}

function func2 {

do stuff
}



}

object = new whatever(blah,blah2);

object->funct1();
object->funct2();

 

  • Like 1
  • Thanks 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×