# Classes.... Right vs Wrong

**URL:** <https://www.phphelp.com/t/classes-right-vs-wrong/29385>\
**Category:** General PHP Help\
**Created:** [July 1, 2019, 12:54pm UTC](https://www.phphelp.com/t/classes-right-vs-wrong/29385 "2019-07-01T12:54:56Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![ChrisCwmbran](https://www.phphelp.com/letter_avatar_proxy/v4/letter/c/7993a0/32.png) [@ChrisCwmbran](https://www.phphelp.com/u/ChrisCwmbran)\
**Post date:** [July 1, 2019, 12:54pm UTC](https://www.phphelp.com/t/classes-right-vs-wrong/29385/1 "2019-07-01T12:54:57Z")

</div>

If I create a class called SampleClass, and give it a method called GetRecord, is there a difference between:

$test = new SampleClass();  
$var1 = $test-\>GetRecord(1);  
$var2 = $test-\>GetRecord(2);  
$var3 = $test-\>GetRecord(3);  
$var4 = $test-\>GetRecord(4);

And:

$var1 = new SampleClass();  
$var2 = new SampleClass();  
$var3 = new SampleClass();  
$var4 = new SampleClass();  
$var1 = $var1-\>GetRecord(1);  
$var1 = $var2-\>GetRecord(2);  
$var1 = $var3-\>GetRecord(3);  
$var1 = $var4-\>GetRecord(4);

I know the latter example is more lines of code, but is either example specifically right or wrong? Is there a reason to use one rather than the other?

Thanks in advance,

Chris.

---

<div class="post-metadata">

**Author:** ![JimL](https://www.phphelp.com/user_avatar/www.phphelp.com/jiml/32/1842_2.png) [@JimL](https://www.phphelp.com/u/JimL)\
**Post date:** [July 1, 2019, 1:20pm UTC](https://www.phphelp.com/t/classes-right-vs-wrong/29385/2 "2019-07-01T13:20:49Z")

</div>

The second example seem to imply something happens with SampleClass when getting a record (as you’re using one instantiation for each get). I assume it’s only a bit wasteful as there is no real reason to use a separate SampleClass instantiation for each get call, but it might be confusing as is.

---

<div class="post-metadata">

**Author:** ![astonecipher](https://www.phphelp.com/user_avatar/www.phphelp.com/astonecipher/32/2797_2.png) [@astonecipher](https://www.phphelp.com/u/astonecipher)\
**Post date:** [July 1, 2019, 1:28pm UTC](https://www.phphelp.com/t/classes-right-vs-wrong/29385/3 "2019-07-01T13:28:24Z")

</div>

The former is operating on a single instance of the class, while the latter is is operating on 4 unique instances. So it depends what you are actually trying to do.

---

<div class="post-metadata">

**Author:** ![ChrisCwmbran](https://www.phphelp.com/letter_avatar_proxy/v4/letter/c/7993a0/32.png) [@ChrisCwmbran](https://www.phphelp.com/u/ChrisCwmbran)\
**Post date:** [July 1, 2019, 1:35pm UTC](https://www.phphelp.com/t/classes-right-vs-wrong/29385/4 "2019-07-01T13:35:40Z")

</div>

Thank you for the responses.

I don’t have any specific reason to instance the class 4 times - I just thought the second way was the easiest way to be honest.

I just couldn’t decide whether there was a real downside to using it the second way. I’m guessing it is primarily a memory overhead issue?

Thanks again,

Chris.

---

<div class="post-metadata">

**Author:** ![chorn](https://www.phphelp.com/letter_avatar_proxy/v4/letter/c/b5ac83/32.png) [@chorn](https://www.phphelp.com/u/chorn)\
**Post date:** [July 1, 2019, 1:42pm UTC](https://www.phphelp.com/t/classes-right-vs-wrong/29385/5 "2019-07-01T13:42:25Z")

</div>

that are two different use cases, you can’t compare them without context.

---

<div class="post-metadata">

**Author:** ![JimL](https://www.phphelp.com/user_avatar/www.phphelp.com/jiml/32/1842_2.png) [@JimL](https://www.phphelp.com/u/JimL)\
**Post date:** [July 1, 2019, 1:54pm UTC](https://www.phphelp.com/t/classes-right-vs-wrong/29385/6 "2019-07-01T13:54:39Z")

</div>

> [@ChrisCwmbran](#):
>
> I’m guessing it is primarily a memory overhead issue?

You’d be surprised how much web servers today can handle. Usually I’m way more concerned about how understandable the code is than how fast it runs. Lots of great talks on youtube on clean code and how important it is as we as devs are really spending more time reading/processing code than we spend writing code. The rather insane amount of bandwidth, cpu, memory and disk io we have available today has kinda made devs lazy. But in fairness hw/bandwidth costs are miniscule compared to dev cost, so it’s kinda to be expected.

---

<div class="post-metadata">

**Author:** ![ChrisCwmbran](https://www.phphelp.com/letter_avatar_proxy/v4/letter/c/7993a0/32.png) [@ChrisCwmbran](https://www.phphelp.com/u/ChrisCwmbran)\
**Post date:** [July 1, 2019, 2:56pm UTC](https://www.phphelp.com/t/classes-right-vs-wrong/29385/7 "2019-07-01T14:56:48Z")

</div>

So is this a more acceptable use of classes? Is it bad to create a Softback object within a method of the Softback object?

```auto
<?php
    spl_autoload_register(function($class) {
        $base_dir = './classes';
        $list_dir = scandir(realpath($base_dir));
        if (isset($list_dir) && !empty($list_dir)) {
            foreach ($list_dir as $list_dir_key => $subDir) {
                $file = $base_dir . DIRECTORY_SEPARATOR . $subDir . DIRECTORY_SEPARATOR . $class . '.php';
                if (file_exists($file)) {
                    require $file;
                }
            }
        }
    });

    $myLibrary = new Library();
    echo "<pre>";
    print_r($myLibrary);
    echo "</pre>";
    die();

class Library
{
    public $book = array();

    public function __construct()
    {
        $item = new Softback;
        $this->book[] = $item->setBook("Bod and Bod!","Janet Smith");
        $this->book[] = $item->setBook("Here comes Bod!","James Jameson");

    }
}

class Softback
{
    public $title = null;
    public $author = null;

    public function setBook($title, $author)
    {
        $result = new Softback();
        $result->title = $title;
        $result->author = $author;
        return $result;
    }
}
```

---

<div class="post-metadata">

**Author:** ![JimL](https://www.phphelp.com/user_avatar/www.phphelp.com/jiml/32/1842_2.png) [@JimL](https://www.phphelp.com/u/JimL)\
**Post date:** [July 1, 2019, 3:11pm UTC](https://www.phphelp.com/t/classes-right-vs-wrong/29385/8 "2019-07-01T15:11:13Z")

</div>

I don’t like that approach. There is no reason “Library” should know how to instantiate Softback objects. And the way Softback actually instantiates a new object of itself is kinda confusing.

I’d much rather iterate over a data set outside the Library class in some way like this. I’d also recommend looking into using composer for auto loading classes.

```php
class Library
{
    private $books = [];

    public function addBook(Book $book)
    {
        $this->books[] = $book;
    }
}

class Softback
{
    private $title = null;
    private $author = null;

    public function __construct($title, $author)
    {
        $this->title = $title;
        $this->author = $author;
    }
}

$library = new Library();
$books = $booksModel->getSoftbackBooks();

foreach ($books as $book) {
  $library->addBook($book);
}

```

Note: for the code above to work you need some sort of books model that can fetch the data. Softback also need to inherit from a parent “book” class so typehinting can be used. I just assumed you’d use multiple kinds of books.

---

<div class="post-metadata">

**Author:** ![benanamen](https://www.phphelp.com/user_avatar/www.phphelp.com/benanamen/32/287_2.png) [@benanamen](https://www.phphelp.com/u/benanamen)\
**Post date:** [July 1, 2019, 4:43pm UTC](https://www.phphelp.com/t/classes-right-vs-wrong/29385/9 "2019-07-01T16:43:41Z")

</div>

> [@JimL](#):
>
> I’d also recommend looking into using composer for auto loading classes.

Like @JimL said, Use Composer and PS-4 Autoloading

---

<div class="post-metadata">

**Author:** ![ChrisCwmbran](https://www.phphelp.com/letter_avatar_proxy/v4/letter/c/7993a0/32.png) [@ChrisCwmbran](https://www.phphelp.com/u/ChrisCwmbran)\
**Post date:** [July 2, 2019, 3:49pm UTC](https://www.phphelp.com/t/classes-right-vs-wrong/29385/10 "2019-07-02T15:49:41Z")

</div>

Thank you for your comments. I’m looking into Composer, and doing so re-coding using what I’ve learnt from you guys - so thank you again.!

JimL - Where you use the following:

public function addBook(Book $book)

which as far as I can see requires $book to be of type Book, if Book had extended classes such as Hardback and Softback is there a way to say it can be either hardback OR softback?

Thanks anyway 🙂

Chris.

---

<div class="post-metadata">

**Author:** ![JimL](https://www.phphelp.com/user_avatar/www.phphelp.com/jiml/32/1842_2.png) [@JimL](https://www.phphelp.com/u/JimL)\
**Post date:** [July 2, 2019, 5:39pm UTC](https://www.phphelp.com/t/classes-right-vs-wrong/29385/11 "2019-07-02T17:39:40Z")

</div>

> [@ChrisCwmbran](#):
>
> which as far as I can see requires $book to be of type Book, if Book had extended classes such as Hardback and Softback is there a way to say it can be either hardback OR softback?

Not yet. Do you have specific logic in the library depending on type of book added?

---

<div class="post-metadata">

**Author:** ![astonecipher](https://www.phphelp.com/user_avatar/www.phphelp.com/astonecipher/32/2797_2.png) [@astonecipher](https://www.phphelp.com/u/astonecipher)\
**Post date:** [July 2, 2019, 5:39pm UTC](https://www.phphelp.com/t/classes-right-vs-wrong/29385/12 "2019-07-02T17:39:45Z")

</div>

> [@ChrisCwmbran](#):
>
> public function addBook(Book $book)
> 
> which as far as I can see requires $book to be of type Book, if Book had extended classes such as Hardback and Softback is there a way to say it can be either hardback OR softback?

You can go a couple ways with that.

You can either do a Book Abstract class that hard and soft extend. Or you can do an Interface. An Interface would allow you to add magazines, movies, and other things that libraries have and everything that implements the interface is allowable.

Interfaces: [GFILHs - Online PHP Interpreter & Debugging Tool - Ideone.com](https://www.ideone.com/GFILHs)
