2

I've recently started to work with OO PHP. As a training practice I'm trying to write some simple classes. I have trouble passing a variable from one to another class. Is it even possible?

class group
{
    public $array = array();

    public function person($name,$surname)
    {
        $this->person = new person($name,$surname);
    }

    public function __destruct()
    {
        print_r($this->array);
    }
}

class person 
{
    public function __construct($name,$surname)
    {
        $this->name = $name;
        $this->surname = $surname;
    }
}

$A = new group();
$A->person("John","Doe");

What I want to archieve here is to pass person as another member of group (by simply putting it in group array) for further modifications and sorting. Been googling around but found nothing.

Please forgive me if it's a dumb one. ;)

1
  • Do you mean: $a->person = new person("John", "Doe"); ? Commented May 20, 2011 at 19:50

3 Answers 3

7

I'm not sure I totally understand but I think you want:

Class group {
    public $members=array();
    public function person($name,$surname) {
        $this->members[]=new person($name,$surname);
        //Creates a new person object and adds it to the internal array.
    }
    /*...*/
}

A better alternative (seperation of intent) would be:

Class group {
    public $members=array();
    public function addPerson(person $p) {
        $this->members[]=$p;
        //Avoids this function need to know how to construct a person object
        // which means you can change the constructor, or add other properties
        // to the person object before passing it to this group.
    }
    /*...*/
}
Sign up to request clarification or add additional context in comments.

8 Comments

@OZ_ OPs code, not mine... otherwise I'd have to explain accessors etc to use the data after.
@OZ_ yes there is, it's named $array I renamed. I certainly think it's worth saying it's not a good approach, but it's not wrong, and I don't want to be writing the code for him.
@Rudu if we can show to him more correct way, we should do it :)
@OZ_ and down voting is your contribution to showing the best way?
@Rudu commented already. If you too lazy to write full and correct answer, it's not a reason to write some piece of smelling code.
|
2

The fix is changing

public function person($name,$surname)
{
    $this->person = new person($name,$surname);
}

to

public function person($name,$surname)
{
    $this->array[] = new person($name,$surname);
}

$this->person is not being stored in the array otherwise, and is overwritten with each call.

Your group class could improve it's OO by:

  1. changing $array to be more descriptively named
  2. changing the function name person to something more meaningful, like add_person

1 Comment

Uh the way you've done this $array is local to the function - this'll do nothing ;)
-2

You should define your properties ('name', 'surname') and give them a suitability visibility

class group
{
    public $array = array();
    public name;
    public surname;
    ...

Reference: http://php.net/manual/en/language.oop5.visibility.php

7 Comments

do not use public properties.
@OZ_: There is no inherent argument against the use of public properties, or else they would not exist. The validity of their use is entirely case-dependent.
@George Cummins no. Public properties violates basis OOP conception - encapsulation.
@OZ_: I think you have misunderstood the role of property visibility in OOP. There is no logical reason to replace all public properties with private properties plus getters/setters, if the getters and setters do not provide any added value. If a particular property needs to be validated for type or content, the validation should occur in the setter. If a property needs to be shielded from modification, logic in the setter can be used to provide protection. If a property's value needs to change based on the manner it is called, the change should occur in a getter. (cont'd)
@OZ_: (cont'd)In cases where the getters and setters add no value, such as: 'public function getProp() { return $prop; }' or 'public function setProp($prop) { $this->prop = $prop; }' there is no justification for the additional overhead. Encapsulation is fine, but there is no difference between an exposed public property and an exposed getter if the getter provides no logic.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.