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

The Archive Base Latest Questions

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

I am creating a collection class and would like it to be drop-in-replacement for

  • 0

I am creating a collection class and would like it to be drop-in-replacement for arrays, which I use currently.

How to create a class which could be casted to boolean, so the class can be truthy or falsy?

A simple test shows that an object of empty class is truthy:

class boolClass {}
$obj = new boolClass();
var_dump( (bool)$obj);
//prints 
//bool(true)

But I need to decide if my class is truthy or falsy. Is there eny way to tell the PHP engine how to cast my class to boolean? Like I could do with __toString()?

Background:

Lets say I write a class like this (it’s an example only):

class MyCollection implements ArrayAccess, Iterator {
    //...
}

I heavily use this patterns currently:

$var = array();

if (empty($var)) {
   //array is empty, (or there is no array at all)
   // I do something here
}

I would like that to look like:

$var = new MyCollection(array());

and keep the rest unchanged. But the $var containing MyCollection is always truthy so I would need to all the conditions to:

if ($var->isEmpty()) {
    //...
}

But this is unacceptable, as my codebase have many megabytes.

Any solution here?

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

    On this page, the magic methods that you can define for your classes are enumerated.

    http://www.php.net/manual/en/language.oop5.magic.php#language.oop5.magic.tostring

    You demonstrate that you already know about __toString().

    Unfortunately, there is no magic method listed there that does what you are asking. So, I think for now your only option is to define a method and call that method explicitly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey im just wondering what would be the best collection to use when creating
I have a model called Category which looks like this: class Category < ActiveRecord::Base
Im creating a custom field in MVC3 (C#) that I would like to access
I would like to ask one interested (for me) question. What collection is the
I am creating an autocomplete service against my mongo db posts collection where a
Has anyone had a go at creating a view that sorts a collection? The
Let's say I'm in a loop creating JPA queries: for(A elem : collection) {
I am creating the security system for the software im creating and I would
I'm creating a tree structure that is based on an AbstractNode class. The AbstractNode
I'm trying to implement a linked collection using generics, something like the following. public

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.