On 3/16/2016 10:12 PM, Björn Larsson wrote:
> Like the RFC :)
> 
> Could this be an opportunity to have default visibility for property's when
> they are typed in a similar fashion like for functions? Meaning no
> visibility
> implies public and code below would work.
> class A {
>   int $i=7;
>   function f() {
>       echo $this->i;
>   }
> }
> $a = new A();
> $a->f();
> echo $a->i;
> 
> Was inspired by discussion in "var" Deprecation thread.
> 
> Regards //Björn
> 

I'd rather like to see this being package visible rather than public. Of
course, that would be yet another Java thingy and I am not trying to
jump on the bandwagon here.

First, I like the Class Friendship RFC because it is very explicit in
terms of giving access to your members. However, it fails to solve some
issues in API design because friendship is not inherited and it is
especially useless in a composer package world where the classes you
might define as a friend are not even available (example follows).

Second, I like the Private Class PR because it is very simple in hiding
a class. The fact that is is purely namespace based is however a problem
because one might want to have a class in a sub-namespace but still have
the ability to instantiate it. Also protected classes would be only
possible by restricting them from the current down to sub-namespaces
which is totally meh.

The introduction of a new `package` keyword to provide access modifiers
for more fine grained control could solve all of the above issues.

    <?php
    // Composer Package `fleshgrinder/best-cms-entity`

    package fleshgrinder/entity;

    namespace Fleshgrinder\Entity;

    use DateTimeImmutable;

    final public class Entity {

        int $id;

        string $name;

        DateTimeImmutable $created;

        DateTimeImmutable $changed;

        private function __construct() {}

        public function equals($other) {
            if ($other instanceof self) {
                return $this->id === $other->id
                    || $this->name === $other->name;
            }
            return false;
        }

    }

    <?php
    // Composer Package `fleshgrinder/best-cms-entity`

    package fleshgrinder/entity;

    namespace Fleshgrinder\Repo;

    use Fleshgrinder\Entity\Entity;

    public interface Repo {

        public function create(string $name): Entity:

    }

    <?php
    // Composer Package `fleshgrinder/best-cms-in-memory-entity-repo`

    package fleshgrinder/entity;

    namespace Fleshgrinder\Repo;

    use DateTimeImmutable;
    use Fleshgrinder\Entity\Entity;

    final public class InMemoryRepo implements Repo {

        private array $entities = [];

        public function create(string $name): Entity {
            $new = new Entity();
            $new->id = count($this->entities) + 1;
            $new->name = $name;
            $new->created = $new->changed = new DateTimeImmutable();

            foreach ($this->entities as $entity) {
                if ($entity->equals($new)) {
                    throw new \Exception;
                }
            }

            $this->entities[] = $new;

            return clone $new;
        }

    }

-- 
Richard "Fleshgrinder" Fussenegger

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to