What will be the better way to access information from another object

I have a page Object,
which has Paragraph and Image object Collections

And each paragraph has only image_id(s) that are assigned to a paragraph. All other information about image is stored in Page->Image

Now from the paragraph object I want to access the image information where paragraph has only image_id and all other information about the image is in Page->Image object.

What will be the better way to access this info?

Should I pass the page object in every paragraph constructor or something else?

I also can’t change the class structure, as it is written by someone else.

1

KISS is always the best approach:

foreach($page->paragraphs as $paragraph)
    foreach($paragraph->image_ids as $image_id)
        $image = $page->images[$image_id]

But if you wanted something a little more special a delegate can assist in providing the “missing” functionality you require and for which php 5.3 has made things super easy by means of a little magic.

<?php

class PageDelegate
{
    private $page;

    public function construct($page)
    {
        $this->page = $page;
    }

    /* The magic delegating methods __call, __set and __get */
    public  function __call($name, $args)
    {
        if (method_exists($this->page, $name))
            return call_user_func_array(array($this->page, $name), $args);
        // you may require some conversion to valid method name
        // like converting - to _ etc. this should be kept in mind.
        if (array_has_key($this->images, $name))
            return $this->images[$name]; // note how this becomes $this->page->images[$name]
        throw BadMethodCallException("Method $name does not exist");
    }
    public function __get($name)
    {
        if (property_exists($this->page, $name))
            return $this->page->{$name};
        throw InvalidArgumentException("Property $name does not exist");
    }
    public function __set($name, $value)
    {
        if (property_exists($this->page, $name))
            $this->page->{$name} = $value;
        else
            throw InvalidArgumentException("Property $name does not exist");
    }

    // And perhaps something to help out with common tasks
    public function images_in_paragraph($paragraph) 
    {
        $images = array();
        foreach($paragraph->image_ids as $image_id)
            // Note how this becomes $this-page->images[$image_id];
            $images[] = $this->{$image_id}();
        // return the collection of image objects in the paragraph
        return $images;
    }
}

Using just the 3 magic methods __call, __set and __get we are effectively delegating to all instance members of Page and you can now call for images using their image id as a method. If there are any Page functionality you want to overwrite, simply adding the function would see it being called instead of our magic __call method to do your bidding. Now it doesn’t matter that you can’t change their source, you can still do what you please. =)

The implementation would then look something like this perhaps

$page = new PageDelegate($page);
foreach($page->paragraphs as $paragraph)
    foreach($page->images_in_paragraph($paragraph) as $image)
        echo "<img title="$image->title" src="$image->src">";

nJoy!

3

The paragraph object need to have some mechanism to know where it’s image data comes from.

Assuming that a paragraph can’t exist in isolation but only as part of a page, there’s no harm in adding the owning page as a constructor parameter for each paragraph in order to supply this.

1

I think rvcoutinho’s comment is in line with the correct answer. Either the page class or a 3rd class (decorator/mediator) should be responsible for providing the image to the paragraph. Any images that the paragraph requires should be passed to the paragraph.

If you don’t like either of those options, I would think that passing the Image Collection to the paragraph is far preferable than passing the page class. After all, if you just think about the problem conceptually, pages contain paragraphs. Paragraphs don’t contain or know about pages.

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật