Hello people, if someone could help me regarding this I would highly appreciate it...

I dont understand classes and have little knowledge regarding classes, I need to know something simple though...

How do I get a value of a variable thats inside a class and inside a custom function in the class ?

Thank you for any help in advance...

    Thank you very much ill try it immidaiately and give some feedback...

      Yes, It is a nice one tutorial.

      thanks for your thanks ( that's about all we ask for and will get in return - if we are lucky ... )
      your parents raised you well, to become a gentleman, TitanKing

      😉

        Hehe, thanks for the kind words, its probably respect that forces one to be polite towards people that keeps the php spirit alive. Its because of guys like you that php is where it is today… always willing to give a helping hand, that’s what I respect, you know you wont get nothing in return you just do it for the love of humanity !

          Sorry but I still cant seem to figure it out... will you please have a look at this simple class :

          Class :

          <?php
          // PHP
          
          // Class Definition
          class Bear {
          	 // define Properties
          
           public $name;
           public $weight;
           public $age;
           public $sex;
           public $color;
          
           // Define Methods
           public function eat() {
           	echo $this->name . " is eating...\n";
           }
            public function run() {
           	echo $this->name . " is running...\n";
           }
            public function kill() {
           	echo $this->name . " is killing...\n";
           }
            public function sleep() {
           	echo $this->color . " is sleeping...\n";
           	$variable = "This is a Test";
           }
          }
          
          ?>
          

          My little test script ;

          <?php
          require_once('object.php');
          // Creating a bear
          $daddy = new Bear;
          $daddy->name = "Daddy Bear";
          $daddy->age  = "8";
          $daddy->sex  = "Male";
          $daddy->color = "black";
          $daddy->weight = 300;
          $daddy->variable = "Testing";
          
          // Another Bear
          $mommy = new Bear;
          $mommy->name = "Mommy Bear";
          $mommy->age  = "7";
          $mommy->sex  = "Female";
          $mommy->color = "brown";
          $mommy->weight = 310;
          
          // ###########################
          
          $daddy->kill();
          $mommy->eat();
          $mommy->sleep();
          
          ?>
          

          Now as you can see in my first script theres a variable $variable, can you echo the data from $variable in my second test script ?

          Thank you indeed, I hope there is a simple solution to this...

            you can not echo $mommy or $daddy
            they are objects, created with 'new'

            but you can echo those class variables, using your objects to do it
            and you can make other variables, using your objects

            // add this at end of your script, to test 
            // $mommy is just an object
            // something you use as reference while working in CLASS
            echo $mommy; 
            
            echo 'heavy '.$mommy->weight; // echoes heavy + weight (310)
            
            $mommysweight = $mommy->weight;
            echo 'weight of mommy = '.$mommysweight;

              I thank you once again, however, I realize that you cannot echo a object, what I actually whant to know is this : In my first script (the object script) I have a Method/Function named sleep () in that function theres a variable reading like this "$variable = "This is a test", how will I echo that value in my second script.

              I appoligize for being so dumb, its just a hurdle I need to jump, then I think I understand OOP, I really appreciate the time you take to awnser my incompetend questions !

                As you help me I am beginning to understand, I cannot thank you enough, OOP is like a wall in front of me I need to break down, if I could only understand the last part, the previous post, I think I have it then ... ! 🙂

                  // yes, your stupid questions make me go crazy *smile*
                  
                  // echo variable
                  echo $mommy->weight;
                  // run a function
                  $mommy->funcname();
                  
                  // examples
                  $daddy->sleep();
                  echo 'linebreak <br>';
                  $daddy->sleep();

                  I have learnt PHP, by trial and errors (many errors)
                  do some experiments, like a scientist
                  and
                  you will make errors
                  but you will learn BETTER, than if somebody JUST TELLS you everything

                  bottomline-> We learn the best from our mistakes!

                  Isn't this === TRUE
                  $That's = Life

                    Write a Reply...