Hello,

I try to create a simple OOP PHP that shows an error:

class_lib.php

<?php

class person {

var $name;

function __constructor($person_name){
    echo "Initialize class";
}

function set_name($new_name){
    $this->name($new_name);
}

function get_name($new_name){
    return $this->name($new_name);  
}

function __destructor(){
    echo "end class";
}
}

?>

program3.php

<?php include("class_lib.php"); ?> <?php

$stefan = new person();
$jimmy = new person();

$stefan->set_name();
$jimmy->set_name();

echo "Stefan Full Name : ".$stefan->get_name();
echo "Jimmy Full Name : ".$jimmy->get_name();

?>

Fatal error: Uncaught ArgumentCountError: Too few arguments to function person::set_name(), 0 passed in C:\xampp\htdocs\TEST\program_3_OOP_PHP.php on line 10 and exactly 1 expected in C:\xampp\htdocs\TEST\class_lib.php:14 Stack trace: #0 C:\xampp\htdocs\TEST\program_3_OOP_PHP.php(10): person->set_name() #1 {main} thrown in C:\xampp\htdocs\TEST\class_lib.php on line 14

line 14: function set_name($new_name){

I am following this tutorial: https://belajarphp.net/belajar-konsep-oop-php/

Thanks in advance.
Can anyone help me fix the error?

Dani AI

Generated

Three small issues are causing the runtime error: the magic method name is wrong, the setter/getter treat a property like a method, and the constructor requires an argument while the code instantiates without one. Below is a compact, correct pattern you can use (visibility, optional constructor arg, and proper property assignment).

class Person {
    private $fullName;

    public function __construct($initial = null) {
        if ($initial !== null) {
            $this->setFullName($initial);
        }
    }

    public function setFullName($name) {
        $this->fullName = trim($name);
    }

    public function getFullName() {
        return $this->fullName;
    }

    public function __destruct() {
        // optional cleanup; usually not needed for simple models
    }
}

Usage examples:

$stefan = new Person();               // constructor ok because default is null
$stefan->setFullName('Stefan');

$jimmy = new Person('Jimmy');         // set via constructor

echo $stefan->getFullName();
echo $jimmy->getFullName();

$stefan = null;                       // forces __destruct() immediately

How this ties back to the thread: was right that passing a name to the setter fixes the immediate call-site error; correctly pointed out the magic methods must be construct and destruct and suggested fixing assignment; and suggested making the constructor parameter optional or calling the setter inside the constructor — this example shows both. Extra tips: use require_once or include_once for class files, prefer visibility (private/protected/public), and use consistent naming (e.g., PascalCase for classes, camelCase for methods).

Recommended Answers

All 7 Replies

$stefan->set_name('stefan');
$jimmy->set_name('jimmy');

Edit __constructor to __construct and __destructor to __destruct.
If you define function with required argument then you cant initialize it without argument e.g.
Constructor with required argument:

function __construct($person_name){
    echo "Initialize class";
    $this->set_name($person_name);
}

Same contructor but argument is optional (set default value):

function __construct($person_name=NULL){
    echo "Initialize class";
    if($person_name !== NULL){
        $this->set_name($person_name);
    }
}

I still getting this error:

Fatal error: Uncaught ArgumentCountError: Too few arguments to function person::__construct(), 0 passed in C:\xampp\htdocs\TEST\program_3_OOP_PHP.php on line 7 and exactly 1 expected in C:\xampp\htdocs\TEST\class_lib.php:10 Stack trace: #0 C:\xampp\htdocs\TEST\program_3_OOP_PHP.php(7): person->__construct() #1 {main} thrown in C:\xampp\htdocs\TEST\class_lib.php on line 10

class_lib.php

<?php

class person {

var $name;

function __construct($person_name){
    echo "Initialize class";
}

function set_name($new_name){
    $this->name($new_name);
}

function get_name($new_name){
    return $this->name($new_name);  
}

function __destruct(){
    echo "end class";
}
}

?>

program3.php

<?php include("class_lib.php"); ?>

<?php

$stefan = new person();
$jimmy = new person();

$stefan->set_name('stefan');
$jimmy->set_name('jimmy');

echo "Stefan Full Name : ".$stefan->get_name();
echo "Jimmy Full Name : ".$jimmy->get_name();

?>

... or call constructor with param:

$stefan = new person('Stefan');

Also edit functions:

function set_name($new_name){
    $this->name = $new_name;
}

and

function get_name(){
    return $this->name;  
}

you can call destructor by set object to NULL e.g.

$stefan = new person();
$jimmy = new person();
$stefan->set_name('stefan');
$jimmy->set_name('jimmy');
echo "Stefan Full Name : ".$stefan->get_name();
echo "Jimmy Full Name : ".$jimmy->get_name();

$stefan = NULL;
$jimmy = NULL;

in your constructor code you ask for a parameter in this case function __construct($person_name){
you're not actually setting person_name in that method but you are requiring it

you should either remove it function __construct(){ from the constructor or default it to null function __construct($person_name=null){

if you keep it you should call set_name($person_name) within your constructor

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.