What mean Models in PHP? [closed] - php

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 9 years ago.
Improve this question
I want to create a RESTful API with PHP and tutorial I found is: http://coreymaynard.com/blog/creating-a-restful-api-with-php/ and everything worked pretty well only in a part of the tutorial did not understand something.
Near the end of the tutorial, when you create a script to run the API does the following:
$Apikey = new Models\APIkey();
$User = new Models\User();
I do not understand what the "new Models\Whatever();" someone could advise me a bit about it and how it works?
In advance I thank you very much.

Aside from the badly worded question, it's not a bad question so I'm not sure what all the abuse in the comments are about.
That syntax is just instantiating a class as normal - the classes in this case being APIKey and User. The only difference is that these classes are in a namespace (Models). You must refer to your class within the namespace it belongs to.
Generally when writing PHP without namespaces, everything exists in the base namespace, so you can refer to classes within the current namespace by just their names. If you have namespace Models written at the top of your page, you can refer to those two classes in your example as just APIKey or User.
If you are within a namespace, and want to instantiate a class that is in the base namespace, you can do this:
namespace Models
$api = new \Api();
Similar to file systems, the preceding \ just refers back to the base namespace.

Related

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.

When to go for a library file in codeigniter [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 8 years ago.
Improve this question
I always use controller files for my project. Sometimes i get confused when to create libraries. Can you please let me know when do we create libraries files and for what purpose?
I can simply define as following:
Controller: This entry point of your application which is associated with URI.
Helper: Helpers are written in procedural format rather than OOP format. Small scale tweaking is handled by this. As the name suggest Helper file helps the main application controllers in some ways.
Library: Libraries are conventionally reusable code which can be used over different projects.
So if you want to write a library that should be reusable and generic. or it will be a waste of time and effort.
When you need to use any code for several portion or projects, you can create library for better understanding and easy access. Suppose, you want to use any user defined captcha generating function, you can create a library and can use it for several projects.
When you are writing some code that is not specific to current project only and it can be reused on other projects then you should create a library.
For example, I have some generic controllers that I use in every project, I put them in libraries and load via __autoload.

Using Java-Style Namespace Convention in PHP or is it better to use X? [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 9 years ago.
Improve this question
I know that Namespaces in PHP are relatively new and so there are many possibilities to use them. Because it was not usable on the Server i did define my classnames like folder_folder_classname if they where in /folder/folder/classname.php
Now i want to change that to get better class names. I thought to redesign my class structure to a java based one, like com\domain\project\folder\folder\class (e.g. in java: com.domain.project.folder.folder package structure) and was thinking that this is a nice solution to avoid any problems with other classes i ever use and don't think about include() for my classes any more :)
So now my Questions:
Is this a good idea or should I use a better system for my php classes?
Is there a common handling for that in php?
(I saw that Zend Framework uses e.g. instead of Zend_Controller_Request_Abstract the namespace Zend\Controller\Request with class name RequestAbstract -> no domain and double wording...)
thanks for your answers :)
I would definitely avoid using the domain wording and just prepend the project name itself like Zend does (that way you avoid collision, too). Since you are thinking about restructuring and if you are using PHP > 5.3.0 (which you should) you might also want to consider the PHP namespaces.

PHP: Class segmentation? [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 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.

Categories