PHP: Class segmentation? [closed] - php

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I'm building an Authentification library that's going to have around 45+ methods for dealing with user related stuff. However I've been wondering if it's actually recommendable to keep everything on a single file.
Is there a benefit on splitting my class into several subclasses and load them when needed?
I can always for example split the class into "mandatory" elements and the elements that only registered users need...
For example:
Mandatory Methods:
$user->is_logged()
$user->login()
$user->register()
Methods for Register...
Methods for logged-in user.

It just depends on how you want to be including the class file(s) in your pages. If you want one simply include() statement for every page, then keep it all in one file. Unless your library is HUGE, the overhead from the other classes shouldn't be too much.
If you do it the other way, you'll simply be including different files based on the status of the session of the client.
Personally, I'd split them up as it's easier to edit them that way, but it's totally up to you.

I'd go with the class/sub-class option.
You could then use a factory to return the correct type of user object based on the current URL or by simply specifying the desired type of user object if your particular setup doesn't lend itself to this.

Related

Laravel: How to read GET_Parameter from URL on EACH page? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am doing a laravel project and want to be able to read a GET Parameter from the URL on each Page. With this optional parameter (url?color=blue) i want to switch between two differen color shemes.
This parameter should be accessable from each page. My function is already prepared and works pretty good. But of course, i won't place this function on each controller and / or view.
So my question is: Is there any posibility to place this function on a central component? Where should i place it?
Session
Perhaps you should consider saving this variable in a session? See the Laravel documentation about this. Sessions provide a way to store information about the user across multiple requests. This seems a solution to what you're trying to achieve persisting the query parameter for every request.
Middleware
To answer your question: you could probably write Middleware to interact with this query parameter. Though it seems counter-intuitive for your use case.
The implementation depends on what you would lke to do with the variable.

PHP - All classes and function in a single page [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
On php, i want to put all classes and functions on a single page. This page will be called throughout my project. is it recommendable or a bad practice?
It is bad practice. It is called a big ball of mud anti pattern. Try to keep everything modularized. It will be more maintainable and readable. You will thank yourself later.
Sure, it's possible. These files are often called "helpers". You could compile all the functions and classes that you need throughout your website in one file (e.g. functions.php). However, you should not forget to properly split your code apart into different files, to prevent creating a big mess. You can then use the functions/classes like so:
require 'functions.php';
$class = new MyClass(); // Use a class from the functions.php
myFunction(); // Use a function from the functions.php
But! Note that this file can now also be accessed via a URL. For example: http://example.com/functions.php. This can, in some cases, cause unwanted behavior. You can restrict the "direct access" to this file using .htaccess rules or simple place this code on top of the helper file:
if(count(get_included_files()) == 1) die();

Laravel 5: constants.php vs constants in controller vs constants in model [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I'm using Laravel 5 and I need to add some immutable values (constants) to be able to make use of them in controllers mostly (maybe in views too). Question is: What's the best approach?
I've been reading and 90% of approaches suggest to use a constants.php and Config.get(), but I don't like this because (I think) a constant is not a config value. I mean, it's not supposed to be changed. In other frameworks, I like to use models or other lib class to define values related to the entity I'm working on, Eg.: I need a constant for cache time in users, then User::CACHE_1_DAY = '86400' (silly example btw).
I would recommend adding a new class containing Helper methods, and bind that class to the application in your AppServiceProvider.
Finally inside that class, place getter methods for the constants.

storing sessions inside database [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Read a lot of posts about storing sessions inside database but no one seems to provide proper information.Then i came across this post and i found it to be very helpful.
http://culttt.com/2013/02/04/how-to-save-php-sessions-to-a-database/
Now what i don't understand with these posts is that they are taking a class named Database which contain certain functions in order to make session class work i.e. to store sessions inside database.
Question : My question to you all is if it is possible please provide me with that database class even it means creating one own database class file.Just to make sure all the functions that the session class is looking inside database class are found and working.
Codepad:http://codepad.org/mtvT3XXB
That article you cite has horrible issues with wording (example: "...we instantiate a copy of the database class...", which is just plain nonsense). But the basic thing behind such "database" class simply is to keep things generic for the reader, which makes sense.
To interact with a database you need some routines for things like connection handling, query execution and preparation and the like. These routines are typically implemented as methods of a class. Such a class is what the author refers to. He does not name a specific one since they are more or less exchangeable.
You don't actually have to implement your own class, you can use one of those already provided. A short overview is given in the php documentation (which you should read!):
http://php.net/manual/en/set.mysqlinfo.php
I suggest you pick the mysqli connector and go through a few tutorials to learn what it does and how to use it.

Set apart functions [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
What is good way:
Keep all functions in one file or to set apart them? (like database.php, session.php)
My approach is split functionalities into classes; and then to put each class into one file.
If you have a lot of functions, I would do the same - split them into thematically separate files and include them only when needed.
If you work with classes, you can make use of PHP's autoloader functionality that will automatically load any PHP files needed to instantiate a certain class.
I prefer using several distinct files -- generally, one per "group of features".
Advantages :
smaller files : easier to deal with in your IDE
several files : one developper can work on one feature, in one file, the other developper on another feature, in another file ; limits the risks of conflicts (yes, even with SVN and equivalents)
you only need to include what's needed
(And if you extend your question to classes : one class per file, structured in directories ; and using autoloading to include the files that are necessary)

Categories