Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set private properties in CodeIgniter\Database\BaseResult::getCustomResultObject() #3051

Closed
ByeongUkChoi opened this issue May 31, 2020 · 5 comments

Comments

@ByeongUkChoi
Copy link
Contributor

CodeIgniter\Database\BaseResult::getCustomResultObject(string $className)
In this function directly access member properties and assign values
like this
$this->customResultObject[$className][$i]->$key = $value;
So, can't set private properties.
But. How to use 'setter' or 'constructor arguments'?

@michalsn
Copy link
Member

This is more like a support question so in the future please post questions on our forum.

Well... obviously constructor arguments are not supported, but you can handle setters via magic __set() method. Anyway, I think you should check the Entity class feature. It will solve something around 99% of your problems: https://codeigniter4.github.io/userguide/models/entities.html

@ByeongUkChoi
Copy link
Contributor Author

ByeongUkChoi commented Jun 17, 2020

In this case not entity, It is std Object.

@michalsn
Copy link
Member

I don't think that stdObject can have private properties, sorry. As I said, you should use custom class or Entity.

@ByeongUkChoi
Copy link
Contributor Author

class CustomClass
{
private $id;
public function setId(id) {$thsi->id = id;}
}
In this case

$this->customResultObject[$className][$i]->$key = $value;
It doesn't works;

@michalsn
Copy link
Member

As I mentioned before - use magic __set() method. Also, if you have more questions, please ask them on our forum.

public function __set($key, $value)
{
	$method = 'set' . ucfirts($key);

	if (method_exists($this, $method))
	{
		$this->$method($value);
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants