Restricting the constructor-access to certain classes - php

Let's say I want to restrict the access to the constructor of a public class, so that only certain classes may create an instance of it, but all classes are able to use the instance.
In Java, I know two ways to archive this:
Make a public static inner class with a private constructor and instantiate it in the outer class.
Make the classes constructor package-private (restricts access to all classes in the same package).
In PHP however, there are no inner classes and the namespaces (which are close to packages) have no own access-modifier (ergo no package-private).
So, how can I protect the access to the classes constructor?

Related

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.

Class method naming issues - conflicts with extended class?

This is less of an issue, and more of a best practice question - I know these are subject to opinion, but I'm sure there must be a standard convention for this particular problem.
Let's say I've got two classes, Account and Associate.
Account contains several methods which are useful to Associate, and so naturally I'd extend the Account class to Associate.
However, an issue of course arises when I have two methods with the same name e.g. create().
So far to counteract this instead of extending the parent class, I've been instantiating it as a variable in the child classes __construct() method, and then calling methods through that, e.g. $this->Account->create();.
Is there another way, e.g. a norm for using an extended classes methods while still having a method of the same name in the child class?
Any answers would be greatly appreciated!
Account contains several methods which are useful to Associate, and so naturally I'd extend the Account class to Associate.
No, this is not naturally, you are misusing inheritance. Or would you say that an associate is an account? If it has an account, use composition. If not, but the account class has methods that are useful for the associate class, they probably shouldn't be in the account class at all. Extract them to one or more other classes which can be used by both, Account and Associate.
The child class method will be called using $this if you are within the child class methods. If you want the parent method though, you'd call parent::create(); from within the child method.
But that is if you absolutely need to extend the Account class, which sounds unnecessary. If the "Account" class has public methods then you don't need to extend it, you can just call them after instantiating the class:
$account = new Account();
$account->create();
Or, if it is a public static method:
$creation = Account::create();

Objects of Class A that inherit behavior of Class B at will (not always) while retaining their own. Possible?

So, I have two classes. I am wondering if I can have two types of objects of Class A:
normal objects of Class A,
objects of class A that inherit Class B's behavior whilst retaining their own,
without creating a third Class.
Is this possible?
Class A in its basic form does not have all the features of Class B, therefore extending Class B is not an option.
I am using PHP btw.
So, what I discovered was that if you want a certain behavior to be shared between different classes (horizontally), this is possible in PHP 5.4+ with the use of traits.
For example, you can define a trait called 'Driving' and various types of Driver actors can implement it.
The classes implementing traits will inherit all the properties and methods of the trait, and those properties and methods will appear as part of the class even when using reflection.
Here's a useful link that explains it well.

What is the difference between Modules in js and Classes in php?

I am learning about modules online and it seems like modules in js and classes in php are very similar. Both group functions together for easier to understand coding. Functions can be declared in both and made public or private. How are they similar in use and how are they different?
Javascript's modules provides some nice features like encapsulation, the private state and even inheritance from other modules. While they provide some of the features of classes, as in PHP, they are not. They try to build on the existing Javascript functonality to emulate classes, hence why the confusion. i.e. they are built to look and feel like classes.
Javascript's modules are instances of an anonymous function assigned to a variable. Therefore they have all the features of a function where their code is executed top to bottom, they have and sometimes use a return statement (in PHP classes no statements can be run directly apart from field definition and assignment) and they even have access to global variables. In PHP, on the other hand a class, or rather it's methods, cannot access a variable that is not in the class itself. In order to access global variables a class method or static function has to explicitly call the variable i.e global $a inorder to import it. In js modules, all global vars are accessible but sometimes one chooses to explicity import them for neater code (function(a){})(imported);
Another important issue is data abstraction. While js modules provide private states for the fields, PHP's classes, just like C++, java, python etc, provide more security to the properties. It allows for base classes using the abstract class and interface keywords whereby class methods and attributes are only defined or structured but not used.
PHP classes also have constructors and destructors, that are called when the class object is initialized and on the last mention used to destroy the object. Granted, you can create functions in modules to run when you want, in PHP on the other hand, functions in the method are only executed when they are called either by the object, the class or other functions.
In classes there are static functions, these can be called without even having an object of the class and run independent of objects, on the other hand in js, everything is an object; which defeats the point of static functions.
They are similar in that: both have inheritance, where you can extend an existing module with a new one, and in PHP you can use extends to inherit from a parent class. They both have private data states preventing external access, they both group and package data and methods, and both are awesome when utilized properly.

PHP Class Property/Method Synchronization

Revised - I have multiple class definitions spread across files (one class per file). What I'm trying to do is this; I have a class called Master which I create an instance of, and in turn the Master object creates instances of the rest of my defined classes. Master has all of my properties defined inside it.
In the instances that Master creates, I want to be able to use Master's properties/methods straight from $this i.e $this->methodInMaster, without the need for passing Master's object to the new instance, storing it, and calling $this->MasterInstance->methodinMaster. Is there any way to achieve this?
The extends keyword does not help me here at all; the classes that Master creates instances of will inherit the properties and methods of Master, but any changes these instances make to the properties of Master are not 'saved' in the Master object, just the object that made the changes. In other words, the changes aren't visible in any other class.
I believe this is a limitation of not making things static so that there are no 'instances', but is there a way to get around all this using the call, set and get magic methods?
Extends doesn't help you because it's not what you need: if you want a single object that is accessible by many instances, you want a Singleton (it has to be static, because the alternative is having some impractical form of sharing a single reference). For the structure you desire, you will need a Singleton that is different from the Master class, but it's accessed by it. Basically you workflow will be:
implement your functionality in the singleton
access the singleton in the methods of Master
extend Master as usual
This way every call in a subclass will be "redirected" transparently to a call to the singleton. It's not very elegant to do this (as opposed to directly accessing the singleton in the subclasses), but it should work for your design.

Categories