Limit which classes can call another class by namespace or interface - php

Is it possible to programmatically limit access to a class methods from another class?
I am working with an MVC framework which is made up of modules. Modules exist in their own namespace and namespace exactly mimics folder structure. A module contains, at least, a class that is an instance of the module interface but usually a controller and a model. Most of them also contain lib classes unique to that module.
For what we need this is quite a sweet setup. A module obviously needs to access the public methods of its own namespaced classes but that means other modules can do that too and they should only be calling the module class public methods.
Can a class method's scope be limited to a single namespace (or by interface) or is this a step too much for PHP's current level of object oriented implementation?

Related

Provide different abstract class to be extended in vendor

A vendor package I'm using has an abstract class that is extended throughout the package, I am trying to add a trait to the abstract class so it is used by each class that extends the abstract class.
What it looks like
abstract class AbstractClass {
}
and
class A extends AbstractClass {
}
What is the correct way to do this?
You can't extend a vendor class directly by modifying its source code (actually, you could but it's a very bad practice). Depending on the context, you have 3 ways to achieve something similar:
1 - Classic inheritance with some methods overriden. Do not forget contracts which allow you to substitute some Laravel classes with yours by implementing these contracts and customize dependency injection.
2 - Using a trait when 1 is not efficient. For example Laravel paginators, Illuminate\Pagination\Paginator and Illuminate\Pagination\LengthAwarePaginator. If you want to extend them with same functionalities, 1 asks 2 identic implementations whereas 2 needs only 1 implementation.
3 - The only way to extend without implementing custom classes is the use of macro/mixin methods from Illuminate\Support\Traits\Macroable. But it's only possible if the base class used this trait.

Scenarios for using Abstract Class and Interface Class

I know the concepts of Abstract Class and Interface Class.
But I want real time example which explains both the concept in our design.
Can anyone help with this?
Both have little different scenerios...
Abstract classes also contains method definition as you know interface does not. But if you defines abstract class you can not inherit more than one classes from child class. But In case of interface you can do that and implement the methods declared inside the interface.
These scenerios are used in the software development as we just declared the signature of the method and then user can write his.her own code inside the method by overriding it.
Here I am mentioning some point what we can do with abstract class and interface
In abstract class you can define abstract methods which should be public or protected. but in Interface you can define public abstract method only.
In abstract class you can define data members and constants but in interface you can define constants only.
In abstract class you can define body of method and you can inherit in sub class, but in interface you can not define body of method.
you can implement multiple interface in a sub class, but you can not inherit multiple abstract classes in a sub class.
Example
In Major Frameworks, uses interfaces for different types of data source class. If we want to make data source class for new introduced database then we must have to implement their interface in our class so it make well maintained code and methods parameters.

How do you document restler api endpoints which are implemented in a base class?

Let's say I have a base class and several derived classes. Base class implements a method that I want exposed on all derived classes, but I want to the swagger docs to reflect each class properly. What is the proper way to do this? Or do I have to override the methods to provide unique documentation for the methods?
If you need the documentation to be unique just override the methods and call parent::{methodName}(); inside and document the overriding method

Model class inside a prestashop module

I am developing a PrestaShop module, which will have it's own database tables. Let's say database table name is 'cat'. So I wanted to have a model class named Cat to keep track of it's properties and related operations. So where should it?
For example, there are prestashop core model classes inside classes directory. Is it ok to create a classes directory inside my module directory for that purpose? will it work?
The standard used is to place the model class in /module/model/YourModelClass.php, you can see this module and in your installation module class you should call it
require_once(_PS_MODULE_DIR_ . 'example/models/YourModelClass.php');
you have not a strict naming standard to your class model, like it does it the controller class and installation class.
Hope that it helps.
Cordially.
PrestaShop model structure is pretty free-flowing. You can decide what structure you want to use.
The only few constraints imposed on you are
having your module class which extends the PrestaShop module class;
registering the appropriate hook and their respective handlers;
My question was about where to place ObjectModel subclasses in prestashop. Above accepted answer is answering that question. But that's not enough to work the module correctly. You will have to include your model class where ever you want to use inside the module.
for example
include_once(_PS_MODULE_DIR_.'mymodule/classes/Cat.php');
class mymoduledisplayModuleFrontController extends ModuleFrontController {
// Other code goes here
}
If you are overriding existing model class, you can put your class inside /modules/your_module/override/classes directory. I have noticed while installing module, your overridden classes will be copied to the prestashop_root/override/classes directory.
http://doc.prestashop.com/display/PS16/Overriding+default+behaviors#Overridingdefaultbehaviors-Overridingaclass

Codeigniter bootstrap like Zend

Is there any way to create a function that works for all controllers in Codeigniter at init?
In Zend there is a application/Bootstrap.php, i need some solution like that.
You could extend the native CI_Controller class and create a MY_Controller class that all of your application's controllers would extend. Methods in the MY_Controller class would then be available to every controller that extends it. You could also put code in the MY_Controller constructor that would be executed each time a child controller was constructed.
I don't remember exactly how the Bootstrap file works in Zend, but if this sounds like a viable solution the Creating Core System Classes section of the documentation explains how to extend the native controller.
You can extend your New_controller to CI_Controller. In New_controller you can write common function which you want. For use about new extended controller you can see this link:
The subject of extending core controllers is discussed briefly in a few places in the manual - specifically in the Core Classes and Creating Libraries pages.
The intent of extending the core Controller is to provide methods and attributes to all your normal Controllers. There are other ways of providing this kind of pervasive functionality - including Hooks, Libraries and Helpers. You should familiarise yourself with the methods and benefits of those alternatives before assuming the following is the answer to your question.
Finally, it’s assumed that you have an application that does something - it doesn’t matter what, merely that you have an existing Controller that we can work with here.
-extend_the_CI_Controller

Categories