I'm not long writing this, I'm questioning if this is the best way to do it

<?php 

class AddShop{
	
public function __construct()
{
    $db = $db = new Database;
    $db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING);
    $this->db = $db;
}

public function address($lat, $lon, $shopName, $streetName, $streetNumber, $town, $region, $country, $postcode, $business, $phone)
{
	$this->db->beginTransaction();
	$insert = $this->db->prepare('INSERT INTO shopAddress (lat, lon, businessName, street, streetNumber, town, region, country, postcode, business, phone) 
			VALUES (:lat,:lon, :shopName, :streetName,  :streetNumber, :town, :region, :country,  :postcode,  :business, :phone)');	
	$insert->execute(array(
	':lat' => $lat,
	':lon' => $lon,
	':shopName' => $shopName,
	':streetNumber' => $streetNumber,
	':country' => $country,
	':streetName' => $streetName,
	':town' => $town,
	':postcode' => $postcode,
	':region' => $region,
	':business' => $business,
	':phone' => $phone,
	));
	$this->db->commit();
}

}

calling it

$newShop = new AddShop();
$newShop->address($lat, $lon, $shopName, $streetName, $streetNumber, $town, $region, $country, $postcode, $business, $phone);
				

cluelessPHP

Does it give you anything that can't be done with

function add_shop($lat, $lon, ...)
{
    $db = new Database();
    ....
    
} add_shop($lat, $lon, $shopName, $streetName, $streetNumber, $town, $region, $country, $postcode, $business, $phone);

If you really did want to use classes, then I would expect you to say, model a Shop and so aggregate the shop data into an object, which you can then save into a database. So, you might write something along these lines (but presumably with more methods that are useful):

<?php
class ShopAddress {
    public function __construct($lat,
                                $lon,
                                $streetName,
                                $streetNumber,
                                $town,
                                $region,
                                $country,
                                $postcode)
    {
        $this->lat = $lat;
        $this->lon = $lon;
        $this->streetName = $streetName;
        $this->streetNumber = $streetNumber;
        $this->town = $town;
        $this->region = $region;
        $this->country = $country;
        $this->postcode = $postcode;
    }
}

class Shop {
    public function __construct($shopName, $address, $business, $phone)
    {
        $this->shopName = $shopName;
        $this->address = $address;
        $this->business = $business;
        $this->phone = $phone;
    }

public function save($db = NULL)
{
    if (is_null($db))
    {
        $db = new Database;
        $db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING);
    }

    $db->beginTransaction();
    $insert = $db->prepare(
        'INSERT INTO shopAddress
         (lat, lon, businessName, street, streetNumber, town,
          region, country, postcode, business, phone)
         VALUES (:lat, :lon, :shopName, :streetName, :streetNumber, :town,
                 :region, :country,  :postcode,  :business, :phone)'
    );
    $insert->execute([
        ':lat' => $this->address->lat,
        ':lon' => $this->address->lon,
        ':shopName' => $this->shopName,
        ':streetNumber' => $this->address->streetNumber,
        ':country' => $this->address->country,
        ':streetName' => $this->address->streetName,
        ':town' => $this->address->town,
        ':postcode' => $this->address->postcode,
        ':region' => $this->address->region,
        ':business' => $this->business,
        ':phone' => $this->phone,
    ]);
    $db->commit();
}
}

Weedpacket's (and Laserlight's) point, I THINK, is that if an Object/Class has only one method, there's no point in it being an Object/Class.

Probably some other stuff, too, but that one's pretty important.

If you can think of more than one thing for the object to do, that's a better use case for an object. For example, in this case, perhaps you'd want a function/method to determine if the latitude/longitude are actually in the country, or that the town is actually in the given region?

Or maybe the form should include a URL, and you could check that a webserver actually responds at that URL.

And don't forget things like logging errors for you to read, and politely notifying the user if one occurs without spilling the gory details ...

dalecosp oh I think I get it now, well I just took the usage part out the full process part looks like this

<?php 
session_start();
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

require ("classes/Database.php");
require ("classes/Shop.php");
require ("classes/ShopAddress.php");
require ("classes/Times.php");


if ($_SERVER['REQUEST_METHOD'] == 'POST')
	{
	$post = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);	
	
$lat = $lon = $uid = $street = $shopName = $streetNumber = $country = $streetName = $town = $postcode = $region = $phone = $business = $sun = $mon = $tue = $wed = $thu = $fri = $sat = $start = $end = $image = "";	
$shopName = $post['shopName'];
$streetNumber = $post['streetNumber'];
$streetName = $post['streetName'];
$town = $post['town'];
$postcode = $post['postcode'];
$phone = $post['phone'];	
$business = $post['business'];	
$region = $post['region'];
$country = $post['country'];
$uid = $post['uid'];
$lat = $post['lat'];
$lon = $post['lon'];

$filename = 'tmp_profile.jpg';
$filepath = 'uploads/images/' . 'tmp_profile.jpg';
$filetype = 'jpg';		

$errors = array();
 
 $fields = array( 
  'shopName' => array( 
    'validator' => 'shopNames', 
    'message'   => 'What is your business name?' 
  ), 

  'streetNumber' => array( 
    'validator' => 'streetNumbers', 
    'message'   => 'Please enter a valid street number' 
  ), 
  'country' => array( 
    'validator' => 'countires', 
    'message'   => 'Which country is your business based in?' 
  ), 
  'streetName' => array( 
    'validator' => 'streetNames', 
    'message'   => 'Which street is your business based in?' 
  ), 
  'town' => array( 
    'validator' => 'towns', 
    'message'   => 'Which town or city is your business based in?' 
  ), 
  'postcode' => array( 
    'validator' => 'postcodes', 
    'message'   => 'Please enter a valid postcode/zipcode' 
  ), 
  'region' => array( 
    'validator' => 'regions', 
    'message'   => 'Which region/state is your business based?' 
  ), 
  'phone' => array( 
    'validator' => 'phones', 
    'message'   => 'Please enter a valid phone number' 
  ), 
  'business' => array( 
    'validator' => 'businesses', 
    'message'   => 'What type of business do you run?' 
  )
);  


foreach($post as $key => $value) 
	{
  if(isset($fields[$key])) 
	{
			if(!Shop::{$fields[$key]['validator']}($value)) 
			{
				$errors[] = ['name' => $key, 'error' => $fields[$key]['message']];
			}
	}
}
	

if(empty($errors)) 
	{
		
	$success = ["message" => "Your shop has been updated"];	
	try
		{
			$newShop = new AddShop();
			$newShop->address($lat, $lon, $shopName, $streetName, $streetNumber, $town, $region, $country, $postcode, $business, $phone, $uid);
		}
		catch (PDOException $e) {
//change back to errors later when finished 
			echo 'Connection failed: ' . $e->getMessage();
			}			

}	  

header('Content-Type: application/json');

if (empty($errors))
{
	echo json_encode($success);
}
else
{
	echo json_encode(["errors" => $errors]);
}			
}	

    8 days later

    Short answer: it depends on what you are trying to accomplish.

    Longer answer: you'll probably find that every single database table you have will end up with similar classes that have a lot of code in common. You might want to create a DBBaseClass for your database objects which perform insert/update/delete and then write cases that extend DBCaseClass and describe the specific columns for each db.

    Other observation: I think Service Locator is inferior to Dependency Injection.

    Write a Reply...