accesing to members of objects

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • julian_m

    accesing to members of objects

    A short piece of code which i'm having problems with:

    class ClsX{

    var $F_name;
    var $F_array_of_obj = array();

    function ClsX(&$obj, $name){
    //Here I add an object reference to an array. Note that all objects
    inside $F_array_of_obj will be of X class or null values
    $this->F_array_of_o bj[] = $obj;
    $this->F_name = $name;
    $this->ListParents( );
    }


    function ListParents(){
    foreach ($this->F_array_of_o bj as $obj) {
    echo "Te name of the parent is: ". $obj->F_name;
    }
    }
    }//class


    $clsX1 = new ClsX("","name1" );

    $clsX2= new ClsX($clsX1, "name2");

    I thought that this code would output

    "the name of the parent is name1"

    but it actually prints

    ""the name of the parent is"

    so the problem is that I can't acces to de object's data inside the
    array.
    Any hints?

    regards - jm

  • Jerry Stuckle

    #2
    Re: accesing to members of objects

    julian_m wrote:[color=blue]
    > A short piece of code which i'm having problems with:
    >
    > class ClsX{
    >
    > var $F_name;
    > var $F_array_of_obj = array();
    >
    > function ClsX(&$obj, $name){
    > //Here I add an object reference to an array. Note that all objects
    > inside $F_array_of_obj will be of X class or null values
    > $this->F_array_of_o bj[] = $obj;
    > $this->F_name = $name;
    > $this->ListParents( );
    > }
    >
    >
    > function ListParents(){
    > foreach ($this->F_array_of_o bj as $obj) {
    > echo "Te name of the parent is: ". $obj->F_name;
    > }
    > }
    > }//class
    >
    >
    > $clsX1 = new ClsX("","name1" );
    >
    > $clsX2= new ClsX($clsX1, "name2");
    >
    > I thought that this code would output
    >
    > "the name of the parent is name1"
    >
    > but it actually prints
    >
    > ""the name of the parent is"
    >
    > so the problem is that I can't acces to de object's data inside the
    > array.
    > Any hints?
    >
    > regards - jm
    >[/color]


    Do you have error reporting enabled? Which version of PHP?

    On 5.0.3 I get:

    Fatal error: Cannot pass parameter 1 by reference in H:\temp\test.ph p on
    line 25

    This is referencing the line:

    $clsX1 = new ClsX("","name1" )

    Which is valid - "" isn't a variable, so you can't reference it.

    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • julian_m

      #3
      Re: accesing to members of objects


      Jerry Stuckle wrote:[color=blue]
      > julian_m wrote:[color=green]
      > > A short piece of code which i'm having problems with:
      > >
      > > class ClsX{
      > >
      > > var $F_name;
      > > var $F_array_of_obj = array();
      > >
      > > function ClsX(&$obj, $name){
      > > //Here I add an object reference to an array. Note that all objects
      > > inside $F_array_of_obj will be of X class or null values
      > > $this->F_array_of_o bj[] = $obj;
      > > $this->F_name = $name;
      > > $this->ListParents( );
      > > }
      > >
      > >
      > > function ListParents(){
      > > foreach ($this->F_array_of_o bj as $obj) {
      > > echo "Te name of the parent is: ". $obj->F_name;
      > > }
      > > }
      > > }//class
      > >
      > >
      > > $clsX1 = new ClsX("","name1" );
      > >
      > > $clsX2= new ClsX($clsX1, "name2");
      > >
      > > I thought that this code would output
      > >
      > > "the name of the parent is name1"
      > >
      > > but it actually prints
      > >
      > > ""the name of the parent is"
      > >
      > > so the problem is that I can't acces to de object's data inside the
      > > array.
      > > Any hints?
      > >
      > > regards - jm
      > >[/color]
      >
      >
      > Do you have error reporting enabled? Which version of PHP?
      >
      > On 5.0.3 I get:
      >
      > Fatal error: Cannot pass parameter 1 by reference in H:\temp\test.ph p on
      > line 25
      >
      > This is referencing the line:
      >
      > $clsX1 = new ClsX("","name1" )
      >
      > Which is valid - "" isn't a variable, so you can't reference it.
      >[/color]



      Well, actually I've solved the problem that I had

      for ($i = 1; $i <= count($this->F_array_of_obj ); $i++) {
      $object = $this->F_array_of_o bj[($i-1)];
      $object->WhatEverObject Function();
      }

      Note that I come from a Delphi background, where all object call should
      be preceded by a typecast. I mean:
      TYPEOFOBJ($obje ct)->WhatEverObject Function();
      In this way, I'm saying "i'm sure that $object has a function named
      WhatEverObjectF unction()"

      BTW, What happens if $object doesn't has WhatEverObjectF unction()?

      regards - jm

      Comment

      • Jerry Stuckle

        #4
        Re: accesing to members of objects

        julian_m wrote:[color=blue]
        > Jerry Stuckle wrote:
        >[color=green]
        >>julian_m wrote:
        >>[color=darkred]
        >>>A short piece of code which i'm having problems with:
        >>>
        >>>class ClsX{
        >>>
        >>>var $F_name;
        >>>var $F_array_of_obj = array();
        >>>
        >>>function ClsX(&$obj, $name){
        >>> //Here I add an object reference to an array. Note that all objects
        >>>inside $F_array_of_obj will be of X class or null values
        >>> $this->F_array_of_o bj[] = $obj;
        >>> $this->F_name = $name;
        >>> $this->ListParents( );
        >>>}
        >>>
        >>>
        >>>function ListParents(){
        >>> foreach ($this->F_array_of_o bj as $obj) {
        >>> echo "Te name of the parent is: ". $obj->F_name;
        >>> }
        >>>}
        >>>}//class
        >>>
        >>>
        >>>$clsX1 = new ClsX("","name1" );
        >>>
        >>>$clsX2= new ClsX($clsX1, "name2");
        >>>
        >>>I thought that this code would output
        >>>
        >>>"the name of the parent is name1"
        >>>
        >>>but it actually prints
        >>>
        >>>""the name of the parent is"
        >>>
        >>>so the problem is that I can't acces to de object's data inside the
        >>>array.
        >>>Any hints?
        >>>
        >>>regards - jm
        >>>[/color]
        >>
        >>
        >>Do you have error reporting enabled? Which version of PHP?
        >>
        >>On 5.0.3 I get:
        >>
        >>Fatal error: Cannot pass parameter 1 by reference in H:\temp\test.ph p on
        >>line 25
        >>
        >>This is referencing the line:
        >>
        >> $clsX1 = new ClsX("","name1" )
        >>
        >>Which is valid - "" isn't a variable, so you can't reference it.
        >>[/color]
        >
        >
        >
        >
        > Well, actually I've solved the problem that I had
        >
        > for ($i = 1; $i <= count($this->F_array_of_obj ); $i++) {
        > $object = $this->F_array_of_o bj[($i-1)];
        > $object->WhatEverObject Function();
        > }
        >
        > Note that I come from a Delphi background, where all object call should
        > be preceded by a typecast. I mean:
        > TYPEOFOBJ($obje ct)->WhatEverObject Function();
        > In this way, I'm saying "i'm sure that $object has a function named
        > WhatEverObjectF unction()"
        >
        > BTW, What happens if $object doesn't has WhatEverObjectF unction()?
        >
        > regards - jm
        >[/color]

        You get a run-time error and processing of the php stops.

        --
        =============== ===
        Remove the "x" from my email address
        Jerry Stuckle
        JDS Computer Training Corp.
        jstucklex@attgl obal.net
        =============== ===

        Comment

        Working...