Help for the PHP Newbies

Hi!

I am new to PHP and started to use Kirby a few months ago. But when I read (well let’s say just taking a look ;)) at the cheatsheet I feel kinda stupid. For example user create . How can I get my head around this and start coding. Is it the best way just to learn PHP from scratch? Or just trying to understand the way Kirby uses PHP?

Thanks for your time to answer this!

1 Like

If you’re looking to code PHP professionally, it may be be worth investing in some training, or certification. I have been doing web development on and off as a hobby for about 15 years. But when I decided a few years ago to make a go at it as a profession I took some certificate courses at O’Reilly School of Technology, to enhance my resume mostly. Their courses eased into each lesson nicely and I would fathom it was very beginner friendly and their instructors we always helpful on the few occasions I got stuck on a problem or assignment, or just wanted to question the methods they used in lieu of other similar methods to achieve specific tasks and why they took that route.

In lieu of that, there are many helpful tutorials and lessons. The vast majority of my knowledge has come from analyzing other people’s code (plugins and such) or reading tutorials. This is just a more scattered approach rather than finding the necessary beginner training from a single source. But rest assured, if you need to know nearly anything, Google already has your answer for free. :slight_smile:

On a side note, that’s an interesting piece of code you’ve chosen to analyze. I’ve never seen try{} used. A quick Google turned up this stack overflow question though. Learn something new every day on these forums. Well, almost.

1 Like

Hello again, as Luke, I’ve been into PHP development for quite a while and always trying to learn something new, mainly by trying to solve a problem with the help of Google and Stackoverflow, by reading and trying to understand other people’s code and by just trying things out.

Let’s just do that with the `$user->create() example:

In a test environment, do the following:

  1. Create a new folder /user-create in /content.
  2. Put a text file in it called user-create.txt
  3. Create a new template called user-create.php and put in the code from the example
  4. Open your browser at yourlocalhostaddress/yourprojectfolder/user-create
  5. See what happens …
  6. You should now have a new account folder in /site/accounts called john

Of course, that in itself doesn’t make too much sense, it just creates one user and if you open the page a second time, you get an error message, because you can’t create the user a second time. So you’d have to combine this bit of code with a form that collects the data and then passes it into the array of the user create method … but one step at a time :wink:

If you don’t want to get too complicated for starters, then you don’t need that much PHP knowledge, because the methods Kirby comes with make many things a whole lot easier than with vanilla PHP, just like using jQuery instead of vanilla JS.

A good starting point, I think, is to try out the different methods and see what they do really …

2 Likes

Thank you alot again! I will give it a try. I am following a course @Lynda, reading the missing PHP manual and always give Google a try. But I think I am missing the basics using the code like provided in the example. I just can’t connect/combine the things I learned into using the code like above. But then again I have to slow down a little and just begin with your example and take step 2 when I am done with one :wink:

You’re welcome :smile:, the thing is, if you do a basic PHP course, you learn all the basics first. And in basic PHP, there is no $user->create(), of course. That is a Kirby method of the $user class which is defined in the user.php like this:

(This is just to demonstrate a bit how this stuff is all connected, pls. don’t get scared away … :flushed:)

static public function create($data = array()) {

    // sanitize the given data for the new user
    $data = static::sanitize($data, 'insert');

    // validate the dataset
    static::validate($data, 'insert');

    // create the file root
    $file = kirby::instance()->roots()->accounts() . DS . $data['username'] . '.php';

    // check for an existing username
    if(file_exists($file)) {
      throw new Exception('The username is taken');
    }

    // create a new hash for the password
    if(!empty($data['password'])) {
      $data['password'] = password::hash($data['password']);
    }

    static::save($file, $data);

    // return the created user project
    return new static($data['username']);

  }

This function, in turn, uses a lot of other functions, which are defined elsewhere in the Kirby core, e.g. a static function called “sanitize”.

If you then look at the static sanitize function just above the user create function,you will find this bit of code:

static public function sanitize($data, $mode = 'insert') {

    // all usernames must be lowercase
    $data['username'] = str::slug(a::get($data, 'username'));

    // return the cleaned up data
    return $data;

  }

Again, there are other functions used here: str::slug and a::getwhich are defined in the Kirby toolkit. The str-thingy is a string manipulation function, and a::get for working with arrays.

Now, if you are still with me on this expedition, you can go visit the Kirby toolkit /kirby/toolkit/lib, where you will find the array methods in a.php and the string methods in str.php. And that’s where we finally find more basic PHP … :laughing:

2 Likes

I am with you! :wink: This is helping me alot! It is like a free fall but I like it.

If anyones reading this in the future, and they happen to have LinkedIn Premium, don’t forget that includes LinkedIn Learning, which is really everything from Lynda.com. (LinkedIn own Lynda.com these days.) Plenty of PHP training in there.

You can also get somewhere for free with CodeAcademy and I can recommend Team Treehouse.

StackSocial also often have great deals on video & eBooks for learning Development.

2 Likes

Tnx!!! Very helpful!

I have basic knowledge of PHP, and for me the best reference is the Kirby documentation.

I encourage the Kirby team to create more articles and tutorials, as far as possible. Video tutorials would be great.

Also as a reference, for anyone learning PHP:

Hi,

I’m new to Kirby and have been using JQuery with WordPress.

I have seen multiple comments here advising to use Vanilla JS over JQuery, I’ve been to Vanilla JS’s website and even searched YouTube and Google for installation tips, I’ve also been searching the Kirby forum but I don’t seem to get some straight forwards information on how to set it up, please can you point me towards a guide or tutorial? Thank you in advance!

If you want to use vanilla JS, there is nothing particular to set up, you can include your script with the js helper to make your life easier: js() | Kirby CMS

If you want to stick with Jquery, you can also include it with this helper. The helper accept the path to the script as argument, e.g

<?= js('assets/js/myscript.js') ?>
1 Like

Vanilla JS usually just refers to using plain JavaScript rather than a bulky framework. What is confusing is there is a framework called Vanilla JS, which could use (although I’m not familiar with it personally). http://vanilla-js.com/

I would recommend using ordinary JavaScript were possible. If you need a little more power / easier life, I would suggest something like the DOMtastic which will give you a jquery like way to target things but with nowhere near the over head. https://domtastic.js.org/

An excellent place to learn vanilla js is the MDN Web docs who have a complete course for free as well as complete reference (like a dictionary) for JavaScript (and CSS and HTML too).

I would also recommend the videos by Net Ninja. These are excellent, concise courses on everything web, all for free. https://www.youtube.com/c/TheNetNinja/playlists

Be sure to check out the Modern Javascript course Modern JavaScript Tutorial #1 - Intro & Setup - YouTube

1 Like

Super helpful! Some good reading and watching for me to do, I really appreciate it. And yes, it seems the framework is what was confusing me.

Another good reference source for many standard tasks (and beyond) is https://plainjs.com/javascript/; they also have a registry of jQuery-free tools/plugins.

…and this site has some handy sample code for replicating core jQuery functionalities in plain JS: http://youmightnotneedjquery.com/

1 Like

That “framework” is a literal joke. If you download it, you get an empty file. All the code examples are plain JS as supported by the browsers. So it‘s just a site that advocates for building sites without JS frameworks. :sweat_smile:

Yes i thought initially it was broken, then i looked more closely at the code :slight_smile: