Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7403743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:09:16+00:00 2026-05-29T05:09:16+00:00

I am new to php oop, I have a some idea re Classes but

  • 0

I am new to php oop, I have a some idea re Classes but I still don’t get around how to use its properties and methods in a created Object. I need to understand if the code reported below is correct, and if not what I do wrong.

I am assuming that I have a Class that can do anything for me. Let’s call it Class myClass {….}

Now I create an object from it and try to work with its methods and properties like this:-

$myObject = new myClass;

$myObject->checkSpeedLight(); // method for checking the speed

if($this->lightSpeed > 10000) echo (“slow down!”); // property defined with a value of 10000

if($this->lightSpeed =< 10000) echo (“Speed up!);

$myObject->keepLightingUp();

$myObject->sleep();

echo ("ligth up");

It has no sense I know, it is just an example. What I need to understand is if the way is written is correct; Any help appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T05:09:17+00:00Added an answer on May 29, 2026 at 5:09 am

    $this is out of context, it can only be used from within the class definition (inside of internal methods etc).

    Outside of the function, we use $myObject->lightspeed;

    Also, I’m assuming that you are setting the lightspeed property with the checkLightSpeed() method.

    EDIT!
    Additionally, it’s considered good practice to have a getter and setter methods. The point is to not access your properties directly, but through an abstraction layer method.

    class MyClass {
        private $property = "Hello World!";
    
        public function getProperty() {
            return $this->property;
        }
    }
    
    $obj = new MyClass();
    $obj->getProperty();
    

    This way you have more control over what, how and when you view your property (for instance, you can require a database connection in order to view it, or restrict access).

    Also note that the property itself is declared private, so direct access from outside the class’s guts is restricted.


    For instance, this is my SpeedOfLight class made in PHP:

    <?php
    
        /**
         * @class                  SpeedOfLight
         *
         * @property $speedOfLight private
         *
         */
        class SpeedOfLight {
    
            private $speedOfLight;
    
            public function checkSpeedOfLight() {
                $this->speedOfLight = 300000000;
            }
    
            public function getSpeedOfLight() {
                return $this->speedOfLight;
            }
    
        }
    
        #Begin testing!
        $obj = new SpeedOfLight();
        $obj->checkSpeedOfLight();
    
        if ($obj->getSpeedOfLight() <= 100000000) {
            echo "Speed up!";
        }
        elseif ($obj->getSpeedOfLight() >= 350000000) {
            echo "Slow down!";
        }
        else {
            echo "Just right!";
        }
    

    (I didn’t know what keepLightSpeed() or sleep() were so I omitted them, but this is the key part).


    Other than that, you’re good.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm pretty new to OOP (and also in PHP programming), and I have some
I'm trying to learn how to use oop in php. I'm also fairly new
I am pretty new to php, but I am learning! I have a simple
i have the oop php code: class a { // with properties and functions
I've been using OOP in PHP for a while now, but for some reason
I'm new to OOP using PHP and the idea seems a little pointless in
I've trying to learn PHP OOP and have made some research on how to
I have been recently learning some OOP PHP and seem to have run into
I'm quite new to PHP OOP and I have a question regarding a simple
Not completely new to PHP itself, but new to using OOP w/ PHP. Object

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.