PHP code example of arashdalir / php-classfriendship

1. Go to this page and download the library: Download arashdalir/php-classfriendship 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/ */

    

arashdalir / php-classfriendship example snippets



namespace Test;

riendship\Friends;
use ArashDalir\ClassFriendship\FriendshipTypes;

class A{
	use Friends;

	protected $parameter;

	function __construct() {
		static::addFriendship(B::class, FriendshipTypes::CAN_READ|FriendshipTypes::CAN_WRITE);
	}
}

class B{
	function testFriendship(){
		$a = new A();

		$a->parameter = "B can access this!";

		print_r($a);
	}
}

class C{
	function testFriendship()
	{
		$a = new A();

		$a->parameter = "C cannot access this! this will throw NotFriendsException";

		print_r($a);
	}
}

$b = new B();
$c = new C();

$b->testFriendship();
$c->testFriendship();

/*
prints:
Test\A Object ( [parameter:protected] => B can access this! )

throws:
 PHP Fatal error:  Uncaught exception 'ArashDalir\ClassFriendship\Exceptions\NotFriendsException' with message 'Class "Test\C" is not a friend of class "Test\A".' in D:\gamp\htdocs\tools\github\ClassFriendship\src\Friends.php:90
 Stack trace:
 #0 D:\gamp\htdocs\tools\github\ClassFriendship\src\Friends.php(62): Test\A->set('parameter', 'C cannot access...')
 #1 D:\gamp\htdocs\tools\github\ClassFriendship\demo.php(34): Test\A->__set('parameter', 'C cannot access...')
 #2 D:\gamp\htdocs\tools\github\ClassFriendship\demo.php(44): Test\C->testFriendship()
 #3 {main}
   thrown in D:\gamp\htdocs\tools\github\ClassFriendship\src\Friends.php on line 90
*/

json
{
  "ashdalir/php-classfriendship": "dev-master"
  }
}