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 582619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:42:22+00:00 2026-05-13T14:42:22+00:00

I’m having serious issues understanding PHP classes from a book. They seem very difficult.

  • 0

I’m having serious issues understanding PHP classes from a book. They seem very difficult. What is their purpose and how do they work?

  • 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-13T14:42:22+00:00Added an answer on May 13, 2026 at 2:42 pm

    In a nutshell, a Class is a blueprint for an object. And an object encapsulates conceptually related State and Responsibility of something in your Application and usually offers an programming interface with which to interact with these. This fosters code reuse and improves maintainability.


    Imagine a Lock:

    namespace MyExample;
    
    class Lock
    {
        private $isLocked = false;
    
        public function unlock()
        {
            $this->isLocked = false;
            echo 'You unlocked the Lock';
        }
        public function lock()
        {
            $this->isLocked = true;
            echo 'You locked the Lock';
        }
        public function isLocked()
        {
            return $this->isLocked;
        }
    }
    

    Ignore the namespace, private and public declaration right now.

    The Lock class is a blueprint for all the Locks in your application. A Lock can either be locked or unlocked, represented by the property $isLocked. Since it can have only these two states, I use a Boolean (true or false) to indicate which state applies. I can interact with the Lock through it’s methods lock and unlock, which will change the state accordingly. The isLocked method will give me the current state of the Lock. Now, when you create an object (also often referred to as an instance) from this blueprint, it will encapsulate unique state, e.g.

    $aLock = new Lock; // Create object from the class blueprint
    $aLock->unlock();  // You unlocked the Lock
    $aLock->lock();    // You locked the Lock
    

    Let’s create another lock, also encapsulating it’s own state

    $anotherLock = new Lock;
    $anotherLock->unlock(); // You unlocked the Lock
    

    but because each object/instance encapsulates it’s own state, the first lock stays locked

    var_dump( $aLock->isLocked() );       // gives Boolean true
    var_dump( $anotherLock->isLocked() ); // gives Boolean false
    

    Now the entire reponsibility of keeping a Lock either locked or unlocked is encaspulated within the Lock class. You don’t have to rebuilt it each time you want to lock something and if you want to change how a Lock works you can change this in the blueprint of Lock instead of all the classes having a Lock, e.g. a Door:

    class Door
    {
        private $lock;
        private $connectsTo;
    
        public function __construct(Lock $lock)
        {
            $this->lock = $lock;
            $this->connectsTo = 'bedroom';
        }
        public function open()
        {
            if($this->lock->isLocked()) {
                echo 'Cannot open Door. It is locked.';
            } else {
                echo 'You opened the Door connecting to: ', $this->connectsTo;
            }
        }
    }
    

    Now when you create a Door object you can assign a Lock object to it. Since the Lock object handles all the responsibility of whether something is locked or unlocked, the Door does not have to care about this. In fact any objects that can use a Lock would not have to care, for instance a Chest

    class Chest
    {
        private $lock;
        private $loot;
    
        public function __construct(Lock $lock)
        {
            $this->lock = $lock;
            $this->loot = 'Tons of Pieces of Eight';
        }
        public function getLoot()
        {
            if($this->lock->isLocked()) {
                echo 'Cannot get Loot. The chest is locked.';
            } else {
                echo 'You looted the chest and got:', $this->loot;
            }
        }
    }
    

    As you can see, the reponsibility of the Chest is different from that of a door. A chest contains loot, while a door separates rooms. You could code the locked or unlocked state into both classes, but with a separate Lock class, you don’t have to and can reuse the Lock.

    $doorLock = new Lock;
    $myDoor = new Door($doorLock);
    
    $chestLock = new Lock;
    $myChest new Chest($chestLock);
    

    Chest and Door now have their unique locks. If the lock was a magical lock that can exist in multiple places at the same time, like in Quantum physics, you could assign the same lock to both chest and door, e.g.

    $quantumLock = new Lock;
    $myDoor = new Door($quantumLock);
    $myChest new Chest($quantumLock);
    

    and when you unlock() the $quantumLock, both Door and Chest would be unlocked.

    While I admit Quantum Locks are a bad example, it illustrates the concept of sharing of objects instead of rebuilding state and responsibility all over the place. A real world example could be a database object that you pass to classes using the database.

    Note that the examples above do not show how to get to the Lock of a Chest or a Door to use the lock() and unlock() methods. I leave this as an exercise for your to work out (or someone else to add).

    Also check When to use self over $this? for a more in-depth explanation of Classes and Objects and how to work with them

    For some additional resources check

    • http://en.wikipedia.org/wiki/Object-oriented_programming
    • http://www.php.net/manual/en/language.oop5.php
    • http://www.tuxradar.com/practicalphp
    • http://www.phpro.org/tutorials/Object-Oriented-Programming-with-PHP.html
    • http://articles.sitepoint.com/article/object-oriented-php
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 299k
  • Answers 299k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer What you are trying to do is enforce non-optimistic concurrency… May 13, 2026 at 7:39 pm
  • Editorial Team
    Editorial Team added an answer That's a tough one, Word isn't able to handle data:… May 13, 2026 at 7:39 pm
  • Editorial Team
    Editorial Team added an answer In Rails 2.3 you've a available_locales method available in module… May 13, 2026 at 7:39 pm

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want use html5's new tag to play a wav file (currently only supported
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I've got a string that has curly quotes in it. I'd like to replace
In order to apply a triggered animation to all ToolTip s in my app,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.