Can I still call object if add function, it's not method of object?

I have a class as below, assuming this is an object:


class house {
		private $windows;
		
		function __construct() {
		
		}
		
		function setWindows() {
		
		}
		
		function getWindows() {
		
		}
		
		function countWindows() {
			// this is object's method
		}
	}

if I add a function like this:


class house {
		private $windows;
		
		function __construct() {
		
		}
		
		function setWindows() {
		
		}
		
		function getWindows() {
		
		}
		
		function countWindows() {
			// this is object's method
			// call isGlassWindows() function here
		}
		
		function isGlassWindows () {
			// this is not object's method
		}
	}

Can I call “house class” is an object?

No you can not, you have to pass the object as a parameter to your function.

But, if this is an object’s method, should I use like this?

I don’t know what you want to do so I’m afraid can’t answer that question. Could you explain a bit more?

I know an object have properties and methods, right. So I’m only want to know I can add any function (not object’s method) for it or not. I see many project they code like #2.

I’m confused by you saying it’s not the object’s method. It’s a method defined within the object; therefore it is the object’s method.

Scallio: Cant it be called by $this->isGlassWindows() ? (My OOP skills are sorely lacking.)

isGlassWindows() is a function, assuming that it’s not object’s method.

Thank all!

My question is what is setWindows going to be doing? This is probably where you will define wether they are glass correct?

Yes, it can

I give an example to everyone to know objects have only properties and methods or not. If I write code with procedure (don’t use class), not object. I don’t need care properties, methods. Do you know what’s object?

hmmm …

Not sure I have completely understood this, but going back to the OP.

Is my object really an instance of a house? Use the function is_a()


$b = new house;

if( is_a($b, "house") ) {
echo 'yep, $b is an object of the house class';

}

Interesting, I didn’t know about is_a(). I’ve always used instanceOf myself:



<?php
$myHouse = new House();

if ($myHouse instanceof House) {
    echo 'Yes, $myHouse is a House';
}
?>


I only know of its existence through using simpleTests’ assertIsA() function.

Blimey, did not know it was deprecated and then subsequently that decision was rescinded. instance of or is_a().

Anyhow, horses for courses – but was that the OPs real question do you think?

Or maybe they are looking for [fphp]method_exists/fphp.

Any instance of “house” will have the method isGlassWindows, I’m not sure why you think the method will not belong to the object. The ONLY way a method will not belong to the object directly is static methods, which are part of the class, not the object.

If you showed us an actual working example of what you are asking, I could give you more answers but in the example you’ve provided, isGlassWindows will belong to the object.