1. Go to this page and download the library: Download rikudou/friend-classes library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
rikudou / friend-classes example snippets
use Rikudou\FriendClasses\Attribute\FriendClass;
#[FriendClass(MyOtherClass::class)]
class ClassWithPrivateProperty
{
private int $myPrivateProperty = 0;
private function myPrivateMethod(): bool
{
return true;
}
}
class MyOtherClass
{
public function someMethod(): void
{
$privateObject = new ClassWithPrivateProperty();
$result = $privateObject->myPrivateProperty;
$result = $privateObject->myPrivateMethod();
}
}
use Rikudou\FriendClasses\Attribute\FriendClass;
#[FriendClass('FriendClass1')]
#[FriendClass('FriendClass2')]
class MyClass
{
}
use Rikudou\FriendClasses\Attribute\HasFriendClasses;
use Rikudou\FriendClasses\Attribute\FriendClass;
#[HasFriendClasses]
class MyPrivateClass {
#[FriendClass(ClassWithAccessToPrivateProperties::class)]
private int $someProperty = 1;
private int $someOtherProperty = 2;
#[FriendClass(ClassWithAccessToPrivateProperties::class)]
private function someMethod(): void
{
// nothing to do
}
private function someOtherMethod(): void
{
// nothing to do
}
}
class ClassWithAccessToPrivateProperties
{
public function __construct()
{
$privateClass = new MyPrivateClass();
var_dump($privateClass->someProperty); // will dump 1
var_dump($privateClass->someOtherProperty); // will throw an error because this class is not a friend
$privateClass->someMethod(); // won't fail
$privateClass->someOtherMethod(); // will throw an error because this class is not a friend
}
}
use Rikudou\FriendClasses\Attribute\FriendClass;
#[FriendClass(Class1::class)]
class PrivateClass
{
#[FriendClass(Class2::class)]
private int $accessibleToBothClasses = 1;
private int $accessibleOnlyToClass1 = 2;
}
class Class1
{
public function __construct()
{
$instance = new PrivateClass();
$instance->accessibleToBothClasses;
$instance->accessibleOnlyToClass1;
var_dump('This will get dumped because Class1 is a friend of the whole class and thus has access to everything');
}
}
class Class2
{
public function __construct()
{
$instance = new PrivateClass();
$instance->accessibleToBothClasses;
$instance->accessibleOnlyToClass1;
var_dump('This will not get dumped because Class2 is only a friend to the $accessibleToBothClasses field');
}
}
use Rikudou\FriendClasses\Attribute\FriendClass;
#[FriendClass('SomeFriendClass')]
class MyClass{private $property = 1;}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.