Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    May 2010
    Posts
    188
    Thanks
    0
    Thanked 0 Times in 0 Posts

    simple oop question

    PHP Code:

    <?php

    class test
    {

    function 
    one()
    {
    $this->a=1;

    }

    function 
    two()
    {
    $this->one();
    echo 
    $this->a;

    }



    }


    $output=new test;
    echo 
    $output->two();

    ?>

    Is there an alternative for $this->one() to call the one method, so I can get the variable $this->a?

  • #2
    New to the CF scene
    Join Date
    Dec 2010
    Location
    Denmark
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Making two() as


    PHP Code:
    function two()
    {
    echo 
    $this->a;

    will still make the var $this->a be echoed as long as it has been set somewhere else in the code.

    I'm not entirely sure what exactly the problem is.
    Last edited by Wizzard of Jazz; 12-21-2010 at 09:05 AM.

  • #3
    New to the CF scene
    Join Date
    Dec 2010
    Location
    Denmark
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    What about

    PHP Code:
    class test
    {

    function 
    __construct(){
    $this->one();
    }

    function 
    one()
    {
    $this->a=1;

    }

    function 
    two()
    {
    echo 
    $this->a;

    }




    Last edited by Wizzard of Jazz; 12-21-2010 at 09:05 AM.

  • #4
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    PHP Code:
    class test
    {
       var 
    $a 1;

       function 
    two()
       {
           echo 
    $this->a;
       }


    $test = new test();
    $test->two(); 

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    var usage is long deprecated.
    PHP is a datatype weak language, so you do not want to allow an exposure of a variable using a class into a public scope ever. It also has a huge disadvantage in for whatever reason they have allowed anonymous writing of properties to classes which makes no sense whatsoever. We won't address this though.
    Because of this datatype weakness, you must write mutators and accessors for your PHP to retain the integrity of the datatype you are working with. Your only options are to call functions, or take the risk that other developers will adhere to your datatype enforcement and expose public members (which I do not recommend, particularly for PHP).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •