PHP Classes

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

    PHP Classes

    Hi everybody,

    I am trying to set up two classes in the following manner:

    class adodb_connectio n
    {
    public function Execute($sSql)
    {
    //do something with $sSql here...
    $this->Execute=new adodb_recordset ;
    $this->Execute->Fields=5;
    }
    }

    class adodb_recordset
    {
    public $Fields;
    }


    When i am trying to get the Fields variable of adodb_recordset at the
    following manner, i get a "Notice: Trying to get property of non-object in
    ..." error.

    =>>>>

    $connMain = new adodb_connectio n;
    //$rstMain = new adodb_recordset ;

    $rstMain = $connMain->Execute('sql') ;
    echo $rstMain->Fields;


    1) Can someone tell me why this isn't working?
    2) If i change $this->Execute->Fields=5; in function Execute to
    $this->Execute->Fieldszzzzzzzz zzzzzzzzzzzz=5; i get no error...! Shouldn't
    php come with an error stating that the variable Fieldszzzzzzzzz zzzzzzzzzzz
    isn't found??


    I hope that someone has the solution for me.

    Thanks in advance..!

    Johnny.



  • =?iso-8859-1?Q?=C1lvaro?= G. Vicario

    #2
    Re: PHP Classes

    *** Johnny escribió/wrote (Thu, 14 Aug 2008 19:25:46 +0200):
    class adodb_connectio n
    {
    public function Execute($sSql)
    {
    //do something with $sSql here...
    $this->Execute=new adodb_recordset ;
    $this->Execute->Fields=5;
    }
    }
    >
    class adodb_recordset
    {
    public $Fields;
    }
    >
    >
    When i am trying to get the Fields variable of adodb_recordset at the
    following manner, i get a "Notice: Trying to get property of non-object in
    .." error.
    >
    =>>>>
    >
    $connMain = new adodb_connectio n;
    //$rstMain = new adodb_recordset ;
    >
    $rstMain = $connMain->Execute('sql') ;
    echo $rstMain->Fields;
    Execute() doesn't return any value so $rstMain is NULL, not an object. I
    guess you mean:

    echo $connMain->Execute->Fields;


    Also, class adodb_connectio n have both a property and a method (undeclared)
    with the same name. Although PHP allows that, it looks pretty confusing.

    2) If i change $this->Execute->Fields=5; in function Execute to
    $this->Execute->Fieldszzzzzzzz zzzzzzzzzzzz=5; i get no error...! Shouldn't
    php come with an error stating that the variable Fieldszzzzzzzzz zzzzzzzzzzz
    isn't found??
    I do get the very same error than above. In any case, in PHP it's optional
    to declare class attributes.



    --
    -- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
    -- Mi sitio sobre programación web: http://bits.demogracia.com
    -- Mi web de humor en cubitos: http://www.demogracia.com
    --

    Comment

    • Johnny

      #3
      Re: PHP Classes

      Hi Álvaro,

      Thank you for your quick response.
      >Execute() doesn't return any value so $rstMain is NULL, not an object.
      After adding the following line to method Execute, no more errors occured:

      return $this->Execute;

      >Also, class adodb_connectio n have both a property and a method
      >(undeclared)
      >with the same name. Although PHP allows that, it looks pretty confusing.
      Do you mean when putting $this->Execute=new adodb_recordset ; in method
      Execute, this isn't referencing the method Execute, but a new variable named
      Execute??

      Thanks,

      Johnny



      "Álvaro G. Vicario" <webmasterNOSPA MTHANKS@demogra cia.comschreef in
      bericht news:1oicobora4 ats.84shtw1toxy 7$.dlg@40tude.n et...
      *** Johnny escribió/wrote (Thu, 14 Aug 2008 19:25:46 +0200):
      >class adodb_connectio n
      >{
      > public function Execute($sSql)
      > {
      > //do something with $sSql here...
      > $this->Execute=new adodb_recordset ;
      > $this->Execute->Fields=5;
      > }
      >}
      >>
      >class adodb_recordset
      >{
      >public $Fields;
      > }
      >>
      >>
      >When i am trying to get the Fields variable of adodb_recordset at the
      >following manner, i get a "Notice: Trying to get property of non-object
      >in
      >.." error.
      >>
      >=>>>>
      >>
      >$connMain = new adodb_connectio n;
      >//$rstMain = new adodb_recordset ;
      >>
      >$rstMain = $connMain->Execute('sql') ;
      >echo $rstMain->Fields;
      >
      Execute() doesn't return any value so $rstMain is NULL, not an object. I
      guess you mean:
      >
      echo $connMain->Execute->Fields;
      >
      >
      Also, class adodb_connectio n have both a property and a method
      (undeclared)
      with the same name. Although PHP allows that, it looks pretty confusing.
      >
      >
      >2) If i change $this->Execute->Fields=5; in function Execute to
      >$this->Execute->Fieldszzzzzzzz zzzzzzzzzzzz=5; i get no error...!
      >Shouldn't
      >php come with an error stating that the variable
      >Fieldszzzzzzzz zzzzzzzzzzzz
      >isn't found??
      >
      I do get the very same error than above. In any case, in PHP it's optional
      to declare class attributes.
      >
      >
      >
      --
      -- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
      -- Mi sitio sobre programación web: http://bits.demogracia.com
      -- Mi web de humor en cubitos: http://www.demogracia.com
      --

      Comment

      • =?iso-8859-1?Q?=C1lvaro?= G. Vicario

        #4
        Re: PHP Classes

        *** Johnny escribió/wrote (Thu, 14 Aug 2008 21:54:11 +0200):
        >>Also, class adodb_connectio n have both a property and a method
        >>(undeclared )
        >>with the same name. Although PHP allows that, it looks pretty confusing.
        >
        Do you mean when putting $this->Execute=new adodb_recordset ; in method
        Execute, this isn't referencing the method Execute, but a new variable named
        Execute??
        Yes. In that sense PHP is different from JavaScript.

        However, it wouldn't make much sense to have a function that commits
        suicide the first time it's called :-?


        --
        -- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
        -- Mi sitio sobre programación web: http://bits.demogracia.com
        -- Mi web de humor en cubitos: http://www.demogracia.com
        --

        Comment

        Working...