2017 © Pedro Peláez
 

library php-classfriendship

This library provides basic functionality needed to simulate class friendship as in C++.

image

arashdalir/php-classfriendship

This library provides basic functionality needed to simulate class friendship as in C++.

  • Tuesday, June 12, 2018
  • by arashdalir
  • Repository
  • 1 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

ClassFriendship

ArashDalir/ClassFriendship provides basic functionality needed to simulate class friendship like in c++., (*1)

Install

Use following command to add the repository to your project:, (*2)

composer require arashdalir/php-classfriendship

Or add following line to your composer.json:, (*3)

{
  "require": {
     "arashdalir/php-classfriendship": "dev-master"
  }
}

Usage

The classes need to use trait Friends in their implementation. A new Friendship can be defined like this:, (*4)

<?php
namespace Test;

include "vendor/autoload.php";

use ArashDalir\ClassFriendship\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
*/

The Versions

12/06 2018

dev-master

9999999-dev

This library provides basic functionality needed to simulate class friendship as in C++.

  Sources   Download

The Requires

  • php ^5.6

 

by Arash Dalir

12/06 2018

v1.0.0

1.0.0.0

This library provides basic functionality needed to simulate class friendship as in C++.

  Sources   Download

The Requires

  • php ^5.6

 

by Arash Dalir