Jump to content

function __autoload


jacob1986

Recommended Posts

I have two questions relating to each other which are regarding - both function __autoload and adding products to a class?

 

Code for an autoload function (please see code #1), the output prints - but also gives an error message? Moreover, I also receive the notification in my php editor "class 'ooo' not found"? If I may ask a silly question I have noticed some autoload fucctions have implode and explode - how would I write one of these?

 

The output and error:

 

My Antonia: Willa Cather (5.99)

Fatal error: Class 'ooo' not found in C:\

 

Code#1:

 

<?php
// we've writen this code where we need
function __autoload($classname) {
    $filename = "./". $classname .".php";
    include_once($filename);
}

// we've called a class ***
$obj = new ooo();
?>

 

------------------------------------------------------------------------------------------------------------------------------

 

 

How would I add more products to this product class?

 

code#2:

 

<?php

class shopProduct {
    public $title;
    protected $producer = [];
    public $price;

    public function __construct($title, $producerName1, $producerName2, $price) {
        $this->title = $title;
        $this->producer[] = $producerName1;
        $this->producer[] = $producerName2;
        $this->price = $price;
    }

    public function getProducer() {
        return implode(' ', $this->producer);
    }
}

class ShopProductWriter {
    public function write ($shopProduct) {
        $str = "{$shopProduct ->title}: " . $shopProduct -> getProducer() . " ({$shopProduct -> price})\n";
        print $str;
    }
}

$product1 = new ShopProduct("My Antonia", "Willa", "Cather", 5.99);
$writer = new ShopProductWriter();
$writer -> write($product1);

?>

 

 

Link to comment
Share on other sites

You would use explode() and implode() in an autoloader to use the class's namespace to map a directory structure. For instance, you would map Foo\Objects\MyObject to /Foo/Objects/MyObject.php by exploding on '\' and imploding with '/'. You're getting the 'class not found' error because you're not loading the file that contains the class definition for ooo().

 

You're not going to add more products to the product class - each instance of the class represents a single product. So, you would have a Shop class that would contain an array of Product classes. (I'm assuming this is from Zandstra's book, as your other post references the book and I remember him using Willa Cather as an example.) I can't remember exactly how he describes using the Product class, but I think he discusses this a bit further on.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.