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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:17:08+00:00 2026-05-30T12:17:08+00:00

I recently started learning the basics of OOP in PHP. I am new to

  • 0

I recently started learning the basics of OOP in PHP.

I am new to a whole lot of concepts.

In the traditional procedural way of doing things, if I had a repetitive task, I wrote a function and called it each time.

Since this seems to be a regular occurence, I created a small library of 5-10 functions, which I included in my procedural projects and used.

In OOP, what is the valid way of using your functions and having them accessible from all objects?

To make things closer to the real world, I created a thumbnail class, that takes an image filename as an argument and can perform some operations on it.

In procedural programming. when I had a function for creating thumbnails, I also had a function to create a random md5 string, check a given folder if said string existed, and repeat if it did, so I could generate a unique name for my thumbnails before saving them.

But if I wanted to generate another unique name for another purpose, say saving a text file, I could call that function again.

So, long story short, what is the valid OOP way to have the method randomise_and_check($filename) (and all other methods in my library) accessible from all the objects in my application?

  • 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-30T12:17:09+00:00Added an answer on May 30, 2026 at 12:17 pm

    Great question. The first thing you want to do is identify the primary objects you will be working with. An easy way to do this is to identify all the nouns related to your project. In your example it sounds like you will be working with images and strings, from this we can create two classes which will contain related attributes (functions, member variables, etc). And as you wisely mentioned, we need to ensure that the algorithms you are converting into OOP can be called from any context, so we try to keep them abstract as possible (within reason).

    So for your specific situation I would suggest something like:

    // Good object reference, abstract enough to cover any type of image
    //  But specific enough to provide semantic API calls
    class Image
    {
        // Using your example, but to ensure you follow the DRY principle
        //  (Don't repeat yourself) this method should be broken up into two
        //  separate methods
        public static function randomise_and_check($fileUri)
        {
            // Your code here
            ....
    
            // Example of call to another class from within this class
            $hash = String::generateHash();
        }    
    }
    
    // Very abstract, but allows this class to grow over time, by adding more 
    //  string related methods
    class String
    {
        public static function generateHash()
        {
            return md5(rand());
        }
    }
    
    // Calling code example
    $imageStats = Image::radomise_and_check($fileUri);
    

    There are several other approaches and ideas that can be employed, such as whether or not to instantiate objects, or whether we should create a parent class from which we can extend, but these concepts will become evident over time and with practice. I think the code snippet provided should give you a good idea what you can do to make the jump from procedural to OOP. And, as always, don’t forget to read the docs for more info.

    — Update —

    Adding an OOP example:

    class Image
    {
        protected $sourceUri;
    
        public function setSourceUri($sourceUri)
        {
            $this->sourceUri = $sourceUri;
        }
    
        public function generateThumb()
        {
            return YourGenerator::resize($this->getSourceUri);
        }
    }
    
    $image = new Image();
    
    $image->setSourceUri($imageUri);
    
    $thumbnail = $image->generateThumbnail();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having only recently started learning the 'new' OpenGL (programmable as opposed to fixed-function, I
I jut recently started learning PHP from a book called PHP/MySQL Programming for the
I've recently started learning/using django; I'm trying to figure out a way to have
I've recently started learning and exploring the basics of GUI programming in Java. Having
I have recently started coding in php again and am learning Object Orientated PHP.
I recently started learning Emacs . I went through the tutorial, read some introductory
I recently started learning Python and I was rather surprised to find a 1000
I recently started learning JavaFX. At the moment I can't build any JavaFX project
I have recently started learning F#, and this is the first time I've ever
I have recently started learning Perl and one of my latest assignments involves searching

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.