I am developing a eCommerce store for school project.
i have several classes like this that uses same database connection.
so i have to separate those.
1. how to use a single database connection file for all of my classes.I have sevaral classes same as this
2. I draw some use case and class diagram. if any one has experience in UML - (ecommerce ) can you verify those?
class abc {
public $id;
public $name;
public $email;
public $db;
function __construct()
{$this->db=new mysqli('localhost','root','','cart');}
public function newsletter_subcribe()
{$sql="insert into table (id,name,email) value('$this->id','$this->name','$this->email')";
$this->db->query($sql);}
this class is do some query to your database for CRUD, the best thing you can do is make one more controller to access to this class, also make sure every function to do that is in private, not public.
so basicly the controller will post data to class to do CRUD.
more like Model in CI.
CONTROLLER -->> YOUR CLASSS -->> DATABASE
CLASS A
{
private function dbconnection()
{
}
private function a($param)
{
dbconnection();
//CRUD HERE
}
}
CLASS B
main function()
{
//load class A here, and you can access all method
$result = a($param);
}
Related
I am trying to get the php class Base to load only once for a custom MVC framework. So that i can close the database connection in the destructor and be able to use it in inheritance but I can't seem to figure it out. I looked at a singleton design here but i cant figure out how to load the database. Is there a way to get this working or perhaps a better way to do this. Or am i going about this in a totally stupid way and should change it?
Base class
class Base {
protected $db;
//constructor
public function Base(){
echo some unique id to test it worked
$db = connection to database
}
//load modules as needed
public function load($m){
$this->$m = new $m;
}
}
module class
class module extends Base {
//some random function
public function listing(){
$this->db->query();
}
}
the index.php which initializes the Base class
$main = new Base;
$main->load( 'module' );
$main->module->listing();
I have a class that contains methods used globally, and am using them by extending the class:
App.php
final class App extends Core {
// The app class handles routing and basically runs the show
}
Core.php
abstract class Core {
public function __construct() { // Here we bring in other classes we use throughout the app
$this->Db = new Db($this);
$this->Mail = new Mail($this);
}
// Then we define multiple methods used throughout the app
public function settings($type) {
// You see this used by the model below
}
}
index.php
$App = new App(); // This fires up the app and allows us to use everything in Core.php
Up until now, this is all great, because everything is handled throughout the site from within $App. However, within my MVC structure, the models need to pull data from the database, as well as retrieve other settings all contained in Core. We do not need the entire $App class to be used by the models, but we need Core to be.
MyModel.php
class MyModel extends Core {
public function welcome() {
return 'Welcome to '.$this->settings('site_name');
}
}
Once MyModel.php comes into play, the Core constructor is run a second time. How do I keep the Core constructor from being run twice?
you can use a static instance in the core class and reuse it.
abstract class Core {
public static $instance; //create a static instance
public function __construct() { // Here we bring in other classes we use throughout the app
$this->Db = new Db($this);
$this->Mail = new Mail($this);
self::$instance = $this; // initialise the instance on load
}
// Then we define multiple methods used throughout the app
public function settings($type) {
// You see this used by the model below
}
}
in the model class, use it like this
class MyModel extends Core {
public function welcome() {
$_core = Core::instance; // get the present working instance
return 'Welcome to '.$_core->settings('site_name');
}
}
you can take a look at this singleton reference
additionally you can check this answer explain-ci-get-instance
Problem:
I used gii to generate database table models. So If I have any change in users table structure, I used gii and all my relations and other methods are removed from class. So I need to make backup of class and regenerate class and bring back other methods and relations.
Possible Solution:
I changed my class into two classes like this for a table 'users':
class Users extends UsersMapper {
public function tableName() {
return 'users';
}
public function rules() {
.....
}
public function relations() {
.....
}
}
class UsersMapper extends CActiveRecord {
public function getAllUsers() {
......
}
public function getBlockedUsers() {
......
}
}
Question:
Above method is working for me and I am using only Users class everywhere in my code. Is it valid method or there is any problem with this logic. Is there any other method.
Thanks
The Giix extension will create a models/Users class and a models/_base/BaseUsers class for your case. The Users class extends the BaseUsers class. Thus only the BaseUsers class needs to be regenerated on changing the database. It also comes with a couple of extra methods that I use quite a lot.
I've built an abstract User class which is extended by Admin, Manager, and Employee. User contains everything that all Users will need, and abstracts everything that each user handles using different logic based on the instance. My confusion comes in when I have functionality that an Admin or Manager class will handle the exact same way, but an Employee won't be able to access at all.
For example, managing a user other than itself should be restricted to only Admins and Managers while an Employee should never be able to do this. I want to avoid copy/pasting the exact same logic in both the Admin and Manager class, so should I make a single private function and move it up to the User class and simply have the Admin/Manager classes call it from there?
abstract class User
{
public $username;
public $userId;
public $company;
public $error;
private function updateUser($user)
{
// Logic for saving the user info
}
....
}
class Admin extends User
{
public function updateUser($user)
{
parent::updateUser($user)
}
....
}
class Manager extends User
{
public function updateUser($user)
{
parent::updateUser($user)
}
....
}
class Employee extends User
{
public function updateUser($user)
{
$this->error = "Invalid Permissions";
}
....
}
Should this be handled differently? Thanks.
You could move the logic into the User base class with a check to ensure that the specified user is either a manager or admin, but you could add different 'classes' of users at a later date which may need the same functionality, in which case the code you wrote will need to be updated.
My suggestion would be a new abstract class that sits in between the Manager/Admin users and the User abstract class, maybe something like UserEditor. UserEditor extends User and provides the functionality for Admin/Managers to update other users, and Admin/Managers extend from UserEditor instead of your User class.
If you do it like this i think that you should have an ACL class and performa a check in the function so that you don't need to override anything
abstract class User
{
public $username;
public $userId;
public $company;
public $error;
private final function updateUser($user)
{
if(ACL::checkIfUserHasPermissionToUpdateUser($this->userId){
//Perform the update, this code will be executed only if the user
//is a Manager or an Admin, but the logic is delegated to the ACL class
}
}
}
Basically I'm looking for feedback or guidance on something I've created this week at work. The problem was that I had two types of document upload. These types both shared methods like upload, isUploaded, move etc. But, in some instances they both had unique functionality.
So I thought the best approach to handle this would be to create an abstract class which contains the common functionality and 2 separate classes which extend the base abstract class in order to inherit the common functionality.
So I have:
abstract class Upload {
protected $_id;
protected $_name;
protected $_dbTable;
abstract public function create(Filter $filter) {}
abstract public function update(Filter $filter) {}
public function __construct($id){
if(!is_null($id)){
$class = new get_called_class();
return new $class($id);
}
}
protected function upload(){
//Code implemented
}
protected function isUploaded(){
//Code implemented
}
protected function move(){
//Code implemented
}
}
Class Book_Upload extends Upload {
$dbTable = 'book';
public function __construct($id){
//Database stuff to obtain record information
//Set protected member variables
$results = $databaseCall();
$this->_id = $results['id'];
$this->_name = $results['name'];
}
public function create(Filter $filter) {
//Code implemented
}
public function update(Filter $filter) {
//Code implemenetd
}
//Other unique functions
}
Class Magazine_Upload extends Upload {
$dbTable = 'magazine';
Same as Booking_Upload but with additional functionality
plus abstract methods
}
My query is, am I using abstract methods correctly? Have I followed the correct path. Also, I'm not sure I need the construct in the abstract class. What if someone attempts to call $upload = new Upload($id)?
Any class should provide a single type of functionality (Single Responsibility Principle, example: Single Responsibility Principle - A hard to see example?).
An upload class must only deal with uploads. Without more code, I smell an over-functional class from your words that tries to accomplish both the upload and document-spesific tasks.
So before going that way, you should define well what these classes will be doing. Are those document-spesific functionalities really related to the actual act of uploading?
You're extending class doesn't call parent::__construct() so the abstract __construct won't make any difference.
You are using abstract classes correctly; they are base classes that are to be built upon by other classes that share common functions and/or will have the same functionality but is implemented differently.
Abstract classes are a base to be built upon that provide common functionality and structure to other classes.